Hi, I''ve just started with Rails and I''m struggling with how to define a particular table relationship in Active Record. There are two tables, Family and Person. Family: id father_id mother_id Person: id name Where the father_id and mother_id are both one-to-one foreign key references back to the Person table. Any advice on how to define this relationship would be appreciated. Thanks. - Adam --~--~---------~--~----~------------~-------~--~----~ 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 Oct 16, 2007, at 7:37 PM, demonGeek wrote:> Hi, > > I''ve just started with Rails and I''m struggling with how to define a > particular table relationship in Active Record. > > There are two tables, Family and Person. > > Family: > id > father_id > mother_id > > Person: > id > name > > Where the father_id and mother_id are both one-to-one foreign key > references back to the Person table. > > Any advice on how to define this relationship would be appreciated. > > Thanks. > > - AdamAssuming that you also have Person: family_id I''d say that you have: class Person belongs_to :family end class Family belongs_to :mother, :class_name => ''Person'', :foreign_key => mother_id belongs_to :father, :class_name => ''Person'', :foreign_key => father_id has_many :people end I think that the :foreign_key option will become the default in Rails 2.0, but it is required in 1.2 (the default being ''person_id'' from the class_name) -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hi Adam Take a look at activescaffold... On Oct 17, 7:37 am, demonGeek <aptompk...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > I''ve just started with Rails and I''m struggling with how to define a > particular table relationship in Active Record. > > There are two tables, Family and Person. > > Family: > id > father_id > mother_id > > Person: > id > name > > Where the father_id and mother_id are both one-to-one foreign key > references back to the Person table. > > Any advice on how to define this relationship would be appreciated. > > Thanks. > > - Adam--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thanks Rob, I did indeed have the person-belongs-to-family relationship too, so your answer solves everything for me. I appreciate the help. - Adam On Oct 16, 6:56 pm, Rob Biedenharn <R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org> wrote:> On Oct 16, 2007, at 7:37 PM, demonGeek wrote: > > > > > > > Hi, > > > I''ve just started with Rails and I''m struggling with how to define a > > particular table relationship in Active Record. > > > There are two tables, Family and Person. > > > Family: > > id > > father_id > > mother_id > > > Person: > > id > > name > > > Where the father_id and mother_id are both one-to-one foreign key > > references back to the Person table. > > > Any advice on how to define this relationship would be appreciated. > > > Thanks. > > > - Adam > > Assuming that you also have > Person: > family_id > > I''d say that you have: > > class Person > belongs_to :family > end > > class Family > belongs_to :mother, :class_name => ''Person'', :foreign_key => > mother_id > belongs_to :father, :class_name => ''Person'', :foreign_key => > father_id > has_many :people > end > > I think that the :foreign_key option will become the default in Rails > 2.0, but it is required in 1.2 (the default being ''person_id'' from > the class_name) > > -Rob > > Rob Biedenharn http://agileconsultingllc.com > R...-xa9cJyRlE0mWcWVYNo9pw7VzRBSg3CFo@public.gmane.org Hide quoted text - > > - Show quoted text ---~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---