I have two models (User and Event) with multiple has_many through associations, where my current association is by the below logic:
- User can participate in many events through EventGroup
- Event has many users through EventGroup
- User can like many events through Eventgroup
- Event has many user likes through Eventgroup
class User has_many : event_groups has_many :events,:through => : event_groups has_many :event_likes,:through => :event_groups,:class_name => "Event" end class Event has_many : event_groups has_many :users,:through => : event_groups has_many :user_likes,:through => :event_groups,:class_name => "User" end class EventGroup belongs_to : user belongs_to :event belongs_to :user_like,:class_name => "User" belongs_to :event_like,:class_name => "Event" end
user_id event_id user_like_id event_like_id
After setting up the association I tried to create the association record with the below code:
user = User.first user.event_likes << Event.first user.save
This is working fine and I can able to get the events liked by that user as
But I am not able to get the User records by
event.user_likes, so I checked my eventgroup record. It has the
nil value for user_like_id.
#<EventGroup id: 24, event_id: 1, user_id: 2,event_like_id: 1, user_like_id: nil>
Let me know the proper way to do this.
Mob: +91 7760780741 | +91 9944414388