Ruby on Rails Thursday, January 24, 2013

Hi All,

I have two models (User and Event) with multiple has_many through associations, where my current association is by the below logic:

  1. User can participate in many events through EventGroup
  2. Event has many users through EventGroup
  3. User can like many events through Eventgroup
  4. 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 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.

regards,
Loganathan
Mob: +91 7760780741 | +91 9944414388
Skype: loganathan.sellappa
ViewMe

--
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.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment