Imagine the following situation: model Person, with the default name, address, location data. Person has either one or no superior which in turn is also a Person model instance. I would expect something like has_one :superior, :class_name => "Person" but where would I place the belongs_to line that goes with it and what arguments do I need to supply? And what kind of foreign keys do I need to specify in my migration? I would like for the following to work: bossman = Person.new :name => "Mr. Bossman" person1 = Person.new :name => "John" person2 = Person.new :name => "Jane" person1.superior = bossman person2.superior = bossman person1.superior.name # => "Mr. Bossman" I wonder what kind of columns I need for this in my Persons table manually specified and which columns are virtual. I experimented a bit already and got something quite similar, but for some reason I was able to modify the person1.superior_id independantly of the person1.superior object. I''d rather have nothing to do with the _id and shield it from being used directly. -- 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 -~----------~----~----~----~------~----~------~--~---
On 18 Apr 2008, at 21:37, Chris Dekker wrote:> > Imagine the following situation: > > model Person, with the default name, address, location data. > Person has either one or no superior which in turn is also a Person > model instance. > > I would expect something like > has_one :superior, :class_name => "Person" > > but where would I place the belongs_to line that goes with it and what > arguments do I need to supply? And what kind of foreign keys do I need > to specify in my migration?I think you just need it to be belongs_to :superior and then it all just works. The foreign key has to sit somewhere and it can''t be on the superior (or else he''d only be able to be the superior of one person). Fred --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> On 18 Apr 2008, at 21:37, Chris Dekker wrote: > >> but where would I place the belongs_to line that goes with it and what >> arguments do I need to supply? And what kind of foreign keys do I need >> to specify in my migration? > I think you just need it to be belongs_to :superior and then it all > just works. The foreign key has to sit somewhere and it can''t be on > the superior (or else he''d only be able to be the superior of one > person). > > FredThanks! From the docs I managed to do this: belongs_to :superior, :class_name => ''Person'', :foreign_key => ''superior_id'' It seems now through saving the object the superior_id and the actual object are linked together as supposed to Thanks! -- 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 -~----------~----~----~----~------~----~------~--~---