Hello, I''ve got the following quiet common domain model. http://55degrees.co.uk/ewan/peopleUserGroups.pdf As you can see a person can create 0 or many groups, a group being created by 1 person. A person belongs to 1 or many groups and a group can contain many people. A can also contain many sub groups. A simple person class to describe these relationship would be... class Person < ActiveRecord::Base #relationships has_many :usergroups has_and_belongs_to_many :usergroups end obviously this is wrong I can''t name both usergroups ''usergroups''. How do I rename the relationship so I can refer to the two relationships. Cheers, Ewan --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
On 10 Dec 2007, at 17:34, ewan wrote:> > Hello, > I''ve got the following quiet common domain model. > > http://55degrees.co.uk/ewan/peopleUserGroups.pdf > > As you can see a person can create 0 or many groups, a group being > created by 1 person. A person belongs to 1 or many groups and a group > can contain many people. A can also contain many sub groups. > > A simple person class to describe these relationship would be... > > class Person < ActiveRecord::Base > #relationships > has_many :usergroups > has_and_belongs_to_many :usergroups > endclass Person has_many :created_groups, :class_name => ''Usergroup'', :foreign_key => ''person_id'' has_and_belongs_to_many :usergroups end should do the trick Fred> > > obviously this is wrong I can''t name both usergroups ''usergroups''. How > do I rename the relationship so I can refer to the two relationships. > Cheers, > Ewan > >--~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
ewan wrote:> Hello, > I''ve got the following quiet common domain model. > > http://55degrees.co.uk/ewan/peopleUserGroups.pdf > > As you can see a person can create 0 or many groups, a group being > created by 1 person. A person belongs to 1 or many groups and a group > can contain many people. A can also contain many sub groups. > > A simple person class to describe these relationship would be... > > class Person < ActiveRecord::Base > #relationships > has_many :usergroups > has_and_belongs_to_many :usergroups > end > > obviously this is wrong I can''t name both usergroups ''usergroups''. How > do I rename the relationship so I can refer to the two relationships.You can name the associations whatever you want, then use the :class_name option to override the implied class name. class Person < ActiveRecord::Base has_many :created_groups, :class_name => "UserGroup" has_and_belongs_to_many :user_groups end class UserGroup < ActiveRecord::Base belongs_to :creator, :class_name => "Person" has_and_belongs_to_many :people end Give your user_groups table a creator_id field, and you should be good to go. -- 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?hl=en -~----------~----~----~----~------~----~------~--~---
Thank you both for your reply and thanks for not just telling me to RTFM. Having just started using Ruby and Rails it''s appreciated having people how are willing to help with the simple questions. Cheers, Ewan http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M001079 --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---