Active Record: Defining Table Relationships

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
Rob@AgileConsultingLLC.com

Hi Adam

Take a look at activescaffold...

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