Ruby on Rails
Sunday, January 27, 2013
Hey Loganathan,
I think the problem is that you trying to model 2 different things throught the same joint model and that does not work, because on the join model you modelling something with 2 different values of events and of users, so you can have values, user_id = 1, event_id = 1, user_like_id = 2, event_id = 2, this represents 2 different users and 2 different events. so basically you have to create to joint models.
class User
has_many :event_appreciations
has_many :event_attendings
has_many :events, :through => :event_attendings
has_many :event_likes, :through => :event_groups, :class_name => "Event"
end
class Event
has_many :event_appreciations
has_many :event_attendings
has_many :users,:through => :event_appreciations
has_many :user_likes, :through => :event_attendings, :class_name => "User"
end
class EventAppreciation
belongs_to :user
belongs_to :event
end
class EventAttending
belongs_to :user
belongs_to :event
end
this should solve your problem,
All the best,
Andre
On Thursday, 24 January 2013 16:40:32 UTC+1, Loganathan Sellappa wrote:
Hi All,
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
Model:
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
EventGroup columns:
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
user.event_likes
.But I am not able to get the User records by
event.user_likes
, so I checked my eventgroup record. It has thenil 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.
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/zam_-xQ41mUJ.
For more options, visit https://groups.google.com/groups/opt_out.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment