I have two models Club and users. users are regular authenticated users and some of them might create a club. A club can also have members (essentially users) and there are attributes and models that apply to member but not to users. Here is what I have right now. Club => belongs_to :user User => has_many clubs (since a user can host multiple clubs). Now how do I fit this member model into the picture, a club can have many member, but since a club only has one user(the user who owns the club), I can''t really do a Club => has_many :users. do i need to create a different model named ClubOwner for users who host clubs or is there a better way to do this. Thanks for reading and helping! -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 14 May 2010 06:50, badnaam <asitkmishra-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have two models Club and users. users are regular authenticated > users and some of them might create a club. A club can also have > members (essentially users) and there are attributes and models that > apply to member but not to users. > > Here is what I have right now. > > Club => belongs_to :user > > User => has_many clubs (since a user can host multiple clubs). > > Now how do I fit this member model into the picture, a club can have > many member, but since a club only has one user(the user who owns the > club), I can''t really do a Club => has_many :users. do i need to > create a different model named ClubOwner for users who host clubs or > is there a better way to do this.You can use something like Club has_many :members, :class_name => ''User'', :foreign_key => ''club_membership_id'' User belongs_to :club, :foreign_key => ''club_membership_id'' However that will only allow a user to be a member of one club, so probably you need a membership joins table so that you can have a club with many members and users in many clubs, each through the memberships table. The rails guide on activeRecord associations will help you. Colin> > Thanks for reading and helping! > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On May 14, 1:50 am, badnaam <asitkmis...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have two models Club and users. users are regular authenticated > users and some of them might create a club. A club can also have > members (essentially users) and there are attributes and models that > apply to member but not to users. > > Here is what I have right now. > > Club => belongs_to :user > > User => has_many clubs (since a user can host multiple clubs). > > Now how do I fit this member model into the picture, a club can have > many member, but since a club only has one user(the user who owns the > club), I can''t really do a Club => has_many :users. do i need to > create a different model named ClubOwner for users who host clubs or > is there a better way to do this.Assuming that a User can belong to many Clubs, you''ll need a join table between the two: class User < AR::Base has_many :owned_clubs, :foreign_key => ''owner_id'', :class_name => ''Club'' has_many :club_memberships, :dependent => :destroy has_many :clubs, :through => :club_memberships end class ClubMembership < AR::Base belongs_to :user belongs_to :club end class Club < AR::Base has_many :club_memberships, :dependent => :destroy has_many :users, :through => :club_memberships belongs_to :owner, :class_name => ''User'' end You''ll need to move your existing ''user_id'' column on Club to ''owner_id''. Hope this helps! --Matt Jones -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Thanks Matt and Colin. On May 14, 8:46 am, Matt Jones <al2o...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On May 14, 1:50 am, badnaam <asitkmis...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > I have two models Club and users. users are regular authenticated > > users and some of them might create a club. A club can also have > > members (essentially users) and there are attributes and models that > > apply to member but not to users. > > > Here is what I have right now. > > > Club => belongs_to :user > > > User => has_many clubs (since a user can host multiple clubs). > > > Now how do I fit this member model into the picture, a club can have > > many member, but since a club only has one user(the user who owns the > > club), I can''t really do a Club => has_many :users. do i need to > > create a different model named ClubOwner for users who host clubs or > > is there a better way to do this. > > Assuming that a User can belong to many Clubs, you''ll need a join > table between the two: > > class User < AR::Base > has_many :owned_clubs, :foreign_key => ''owner_id'', :class_name => > ''Club'' > has_many :club_memberships, :dependent => :destroy > has_many :clubs, :through => :club_memberships > end > > class ClubMembership < AR::Base > belongs_to :user > belongs_to :club > end > > class Club < AR::Base > has_many :club_memberships, :dependent => :destroy > has_many :users, :through => :club_memberships > belongs_to :owner, :class_name => ''User'' > end > > You''ll need to move your existing ''user_id'' column on Club to > ''owner_id''. > > Hope this helps! > > --Matt Jones > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
I am guessing I will need to change the routes like.. map.resources :users, has_many => :owned_clubs? On May 14, 7:06 pm, badnaam <asitkmis...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Thanks Matt and Colin. > > On May 14, 8:46 am, Matt Jones <al2o...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > On May 14, 1:50 am, badnaam <asitkmis...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > I have two models Club and users. users are regular authenticated > > > users and some of them might create a club. A club can also have > > > members (essentially users) and there are attributes and models that > > > apply to member but not to users. > > > > Here is what I have right now. > > > > Club => belongs_to :user > > > > User => has_many clubs (since a user can host multiple clubs). > > > > Now how do I fit this member model into the picture, a club can have > > > many member, but since a club only has one user(the user who owns the > > > club), I can''t really do a Club => has_many :users. do i need to > > > create a different model named ClubOwner for users who host clubs or > > > is there a better way to do this. > > > Assuming that a User can belong to many Clubs, you''ll need a join > > table between the two: > > > class User < AR::Base > > has_many :owned_clubs, :foreign_key => ''owner_id'', :class_name => > > ''Club'' > > has_many :club_memberships, :dependent => :destroy > > has_many :clubs, :through => :club_memberships > > end > > > class ClubMembership < AR::Base > > belongs_to :user > > belongs_to :club > > end > > > class Club < AR::Base > > has_many :club_memberships, :dependent => :destroy > > has_many :users, :through => :club_memberships > > belongs_to :owner, :class_name => ''User'' > > end > > > You''ll need to move your existing ''user_id'' column on Club to > > ''owner_id''. > > > Hope this helps! > > > --Matt Jones > > > -- > > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en. > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.