# Active Record: Defining Table Relationships

**URL:** <https://www.ruby-forum.com/t/active-record-defining-table-relationships/117457>\
**Category:** Rails\
**Created:** [October 17, 2007, 1:37am UTC](https://www.ruby-forum.com/t/active-record-defining-table-relationships/117457 "2007-10-17T01:37:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![demonGeek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/96bed5/32.png) [@demonGeek](https://www.ruby-forum.com/u/demonGeek)\
**Post date:** [October 17, 2007, 1:37am UTC](https://www.ruby-forum.com/t/active-record-defining-table-relationships/117457/1 "2007-10-17T01:37:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![demonGeek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/96bed5/32.png) [@demonGeek](https://www.ruby-forum.com/u/demonGeek)\
**Post date:** [October 17, 2007, 2:57am UTC](https://www.ruby-forum.com/t/active-record-defining-table-relationships/117457/2 "2007-10-17T02:57:14Z")

</div>

On Oct 16, 2007, at 7:37 PM, demonGeek wrote:

> mother\_id  
> 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 B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![demonGeek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/96bed5/32.png) [@demonGeek](https://www.ruby-forum.com/u/demonGeek)\
**Post date:** [October 17, 2007, 6:17am UTC](https://www.ruby-forum.com/t/active-record-defining-table-relationships/117457/3 "2007-10-17T06:17:04Z")

</div>

Hi Adam

Take a look at activescaffold…

---

<div class="post-metadata">

**Author:** ![demonGeek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/96bed5/32.png) [@demonGeek](https://www.ruby-forum.com/u/demonGeek)\
**Post date:** [October 17, 2007, 7:02am UTC](https://www.ruby-forum.com/t/active-record-defining-table-relationships/117457/4 "2007-10-17T07:02:03Z")

</div>

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 B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
