I have a relations question. I have an events table where each event is owned by a user. So... class Event < ActiveRecord::Base belongs_to :user end And a user... class User < ActiveRecord::Base has_many :events end But, I also have an attendance_lists table so that other users can say they are attending an event (or not attending). So... class AttendanceList < ActiveRecord::Base belongs_to :users belongs_to :events end But now, if I''m going to add the relations to the user and event models, it would seem I would have... class Event < ActiveRecord::Base belongs_to :user has_many :attendance_lists has_many :users, :through => :attendance_lists end class User < ActiveRecord::Base has_many :events has_many :attendance_lists has_many :events, :through => :attendance_lists end My question is whether ActiveRecord will be able to differentiate, using the relations above, the events a user is in charge of and events a user is attending. In the user model, will having two has_many :events, even though one has a :through clause, confuse ActiveRecord? Am I confused and not including something I should be? --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Dylan Bennett wrote:> I have a relations question. I have an events table where each event is > owned by a user. So... > > class Event < ActiveRecord::Base > belongs_to :user > end > > And a user... > > class User < ActiveRecord::Base > has_many :events > end > > But, I also have an attendance_lists table so that other users can say > they are attending an event (or not attending). So... > > class AttendanceList < ActiveRecord::Base > belongs_to :users > belongs_to :events > end > > But now, if I''m going to add the relations to the user and event > models, it would seem I would have... > > class Event < ActiveRecord::Base > belongs_to :user > has_many :attendance_lists > has_many :users, :through => :attendance_lists > end > class User < ActiveRecord::Base > has_many :events > has_many :attendance_lists > has_many :events, :through => :attendance_lists > end > > My question is whether ActiveRecord will be able to differentiate, > using the relations above, the events a user is in charge of and events > a user is attending. In the user model, will having two has_many > :events, even though one has a :through clause, confuse ActiveRecord? > Am I confused and not including something I should be?Just name the associations so that you (and ActiveRecord) can distinguish them. By default AR will infer things like class and foreign key from the association name, but you can override those things using :class_name and :foreign_key options on the association. I have many examples of doing that on my blog, particularly these: http://blog.hasmanythrough.com/articles/2006/04/21/self-referential-through http://blog.hasmanythrough.com/articles/2006/05/06/through_gets_uniq -- Josh Susser http://blog.hasmanythrough.com -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Thanks! I actually dug through your blog looking for something, but I think I just didn''t know what I was looking for yet. I replaced this... has_many :events, :through => :attendance_lists ...with this... has_many :events_to_attend, :through => :attendance_lists, :foreign_key => "event_id", :class_name => "Event" Thanks for the help! --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Okay, looking at this again, I think I was wrong. Looking in the API docs, it seems I would need to do... has_many :events_to_attend, :through => :attendance_lists, :source => :event Is that right? --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---