I have two classes: a Widget and a User. The User has an id that is referenced in two places on the Widget: owner_id and operator_id. How should I structure that reference for has_many? Thanks folks - I appreciate any help on this. --David --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
widget.rb has_many :owners, :class_name => "user", :foreign_key => "owner_id" has_many :operators, :class_name => "user", :foreign_key => "operator_id" then in the widget database you have those two fields ''owner_id" and "operator_id" :) On Jun 13, 11:15 am, David <drali...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have two classes: a Widget and a User. The User has an id that is > referenced in two places on the Widget: owner_id and operator_id. > > How should I structure that reference for has_many? > > Thanks folks - I appreciate any help on this. > > --David--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Just what I needed - thanks sw0rdfish. The books I have were not real clear on this. --David On Jun 13, 11:41 am, sw0rdfish <san...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> widget.rb > > has_many :owners, :class_name => "user", :foreign_key => "owner_id" > has_many :operators, :class_name => "user", :foreign_key => > "operator_id" > > then in the widget database you have those two fields ''owner_id" and > "operator_id" > > :) > > On Jun 13, 11:15 am, David <drali...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I have two classes: a Widget and a User. The User has an id that is > > referenced in two places on the Widget: owner_id and operator_id. > > > How should I structure that reference for has_many? > > > Thanks folks - I appreciate any help on this. > > > --David--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hmmm - now that I look at it, should it be belongs to? widget.rb belongs_to: owners, :class_name => "user", :foreign_key => "owner_id" belongs_to: operators, :class_name => "user", :foreign_key => "operator_id" ? Each widget has a single owner and operator. A user can be the owner and/or operator of many widgets. --David On Jun 13, 11:41 am, sw0rdfish <san...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> widget.rb > > has_many :owners, :class_name => "user", :foreign_key => "owner_id" > has_many :operators, :class_name => "user", :foreign_key => > "operator_id" > > then in the widget database you have those two fields ''owner_id" and > "operator_id" > > :) > > On Jun 13, 11:15 am, David <drali...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I have two classes: a Widget and a User. The User has an id that is > > referenced in two places on the Widget: owner_id and operator_id. > > > How should I structure that reference for has_many? > > > Thanks folks - I appreciate any help on this. > > > --David--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 13 Jun 2008, at 16:50, David wrote:> > Hmmm - now that I look at it, should it be belongs to? > > widget.rb > > belongs_to: owners, :class_name => "user", :foreign_key => "owner_id" > belongs_to: operators, :class_name => "user", :foreign_key => > "operator_id" >it''s belongs_to (and then the corresponding has_many on user). You need to use singulars though and the class name should be the actual class name, ie belongs_to :owner, :class_name => "User" (the foreign key is inferred from the association name) I wrote this up in detail: http://www.spacevatican.org/2008/5/6/creating-multiple-associations-with-the-same-table Fred> ? > > Each widget has a single owner and operator. A user can be the owner > and/or operator of many widgets. > > --David > > On Jun 13, 11:41 am, sw0rdfish <san...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> widget.rb >> >> has_many :owners, :class_name => "user", :foreign_key => "owner_id" >> has_many :operators, :class_name => "user", :foreign_key => >> "operator_id" >> >> then in the widget database you have those two fields ''owner_id" and >> "operator_id" >> >> :) >> >> On Jun 13, 11:15 am, David <drali...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >>> I have two classes: a Widget and a User. The User has an id that is >>> referenced in two places on the Widget: owner_id and operator_id. >> >>> How should I structure that reference for has_many? >> >>> Thanks folks - I appreciate any help on this. >> >>> --David > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Yeah Fred got ya... I just based it on your title, and assumed ( shame on me :( ) If a user has_many widgets then yeah m use belongs_to and owner, not owners. Writeup looks good too... have a look at it. On Jun 13, 12:04 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 13 Jun 2008, at 16:50, David wrote: > > > > > Hmmm - now that I look at it, should it be belongs to? > > > widget.rb > > > belongs_to: owners, :class_name => "user", :foreign_key => "owner_id" > > belongs_to: operators, :class_name => "user", :foreign_key => > > "operator_id" > > it''s belongs_to (and then the corresponding has_many on user). You > need to use singulars though and the class name should be the actual > class name, ie > belongs_to :owner, :class_name => "User" (the foreign key is inferred > from the association name) > > I wrote this up in detail:http://www.spacevatican.org/2008/5/6/creating-multiple-associations-w... > > Fred > > > ? > > > Each widget has a single owner and operator. A user can be the owner > > and/or operator of many widgets. > > > --David > > > On Jun 13, 11:41 am, sw0rdfish <san...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> widget.rb > > >> has_many :owners, :class_name => "user", :foreign_key => "owner_id" > >> has_many :operators, :class_name => "user", :foreign_key => > >> "operator_id" > > >> then in the widget database you have those two fields ''owner_id" and > >> "operator_id" > > >> :) > > >> On Jun 13, 11:15 am, David <drali...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > >>> I have two classes: a Widget and a User. The User has an id that is > >>> referenced in two places on the Widget: owner_id and operator_id. > > >>> How should I structure that reference for has_many? > > >>> Thanks folks - I appreciate any help on this. > > >>> --David--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Fred, great write up - thanks for taking the time to put that up! --David On Jun 13, 3:47 pm, sw0rdfish <san...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Yeah Fred got ya... I just based it on your title, and assumed ( shame > on me :( ) > > If a user has_many widgets then yeah m use belongs_to and owner, not > owners. > > Writeup looks good too... have a look at it. > > On Jun 13, 12:04 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > On 13 Jun 2008, at 16:50, David wrote: > > > > Hmmm - now that I look at it, should it be belongs to? > > > > widget.rb > > > > belongs_to: owners, :class_name => "user", :foreign_key => "owner_id" > > > belongs_to: operators, :class_name => "user", :foreign_key => > > > "operator_id" > > > it''s belongs_to (and then the corresponding has_many on user). You > > need to use singulars though and the class name should be the actual > > class name, ie > > belongs_to :owner, :class_name => "User" (the foreign key is inferred > > from the association name) > > > I wrote this up in detail:http://www.spacevatican.org/2008/5/6/creating-multiple-associations-w... > > > Fred > > > > ? > > > > Each widget has a single owner and operator. A user can be the owner > > > and/or operator of many widgets. > > > > --David > > > > On Jun 13, 11:41 am, sw0rdfish <san...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > >> widget.rb > > > >> has_many :owners, :class_name => "user", :foreign_key => "owner_id" > > >> has_many :operators, :class_name => "user", :foreign_key => > > >> "operator_id" > > > >> then in the widget database you have those two fields ''owner_id" and > > >> "operator_id" > > > >> :) > > > >> On Jun 13, 11:15 am, David <drali...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > >>> I have two classes: a Widget and a User. The User has an id that is > > >>> referenced in two places on the Widget: owner_id and operator_id. > > > >>> How should I structure that reference for has_many? > > > >>> Thanks folks - I appreciate any help on this. > > > >>> --David--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---