# Models that reference themselves

**URL:** <https://www.ruby-forum.com/t/models-that-reference-themselves/117047>\
**Category:** Rails\
**Created:** [October 12, 2007, 12:39pm UTC](https://www.ruby-forum.com/t/models-that-reference-themselves/117047 "2007-10-12T12:39:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![DavidH](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a4c791/32.png) [@DavidH](https://www.ruby-forum.com/u/DavidH)\
**Post date:** [October 12, 2007, 12:39pm UTC](https://www.ruby-forum.com/t/models-that-reference-themselves/117047/1 "2007-10-12T12:39:58Z")

</div>

Hi

I’m pretty much a newbie to Rails and I have at least two projects  
under development that are going to need the same type of constructs  
and I’m struggling to get my head around how to achieve what I want  
using Rails models and relationships.

In one project I have a hierarchy of (geographical) sites. For  
example:

Earth  
Europe  
Ireland  
Cork  
Kerry  
Galway  
…  
UK  
France  
America

If I were using any other language my sites model would look like:

id, # Primary Key  
site\_name,  
parent\_id # references the id of this site’s parent.

In Rails I think I need to:

1. Drop the parent\_id column
2. Use a bridge table to bridge the site id back to the parent site  
record and
3. A has\_and\_belongs\_to\_many relationship

Is this the correct approach?

I’m sure this must have been done before as I have two projects that I  
want to do the same thing in. I’ve found examples in the wiki of  
bridging two tables in this way; but I’ve not found a way of linking a  
table back to itself.

Any assistance will be gratefully appreciated.

Regards

David

---

<div class="post-metadata">

**Author:** ![DavidH](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a4c791/32.png) [@DavidH](https://www.ruby-forum.com/u/DavidH)\
**Post date:** [October 12, 2007, 1:15pm UTC](https://www.ruby-forum.com/t/models-that-reference-themselves/117047/2 "2007-10-12T13:15:03Z")

</div>

On 12 Oct 2007, at 11:39, DavidH wrote:

> 1. Use a bridge table to bridge the site id back to the parent site  
> record and
> 2. A has\_and\_belongs\_to\_many relationship
> 
> Is this the correct approach?

If a site has only one parent then you should stick with parent\_id.

You then stick  
belongs\_to :parent, :class\_name =\> ‘Site’, :foreign\_key =\> ‘parent\_id’

in your Site model

Fred

---

<div class="post-metadata">

**Author:** ![DavidH](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a4c791/32.png) [@DavidH](https://www.ruby-forum.com/u/DavidH)\
**Post date:** [October 12, 2007, 2:04pm UTC](https://www.ruby-forum.com/t/models-that-reference-themselves/117047/3 "2007-10-12T14:04:48Z")

</div>

Many thanks Frederick. Yes a site can only have one parent. I will  
give your suggestion a try and see how I get on.

Cheers

David

On Oct 12, 12:13 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![DavidH](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a4c791/32.png) [@DavidH](https://www.ruby-forum.com/u/DavidH)\
**Post date:** [October 12, 2007, 2:10pm UTC](https://www.ruby-forum.com/t/models-that-reference-themselves/117047/4 "2007-10-12T14:10:40Z")

</div>

On 10/12/07, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > In Rails I think I need to:  
> > You then stick  
> > belongs\_to :parent, :class\_name =\> ‘Site’, :foreign\_key =\> ‘parent\_id’
> 
> in your Site model

May also want to add:  
has\_many :children, :class\_name =\>“Site”, :foreign\_key =\> “parent\_id”

You could also look at acts\_as\_tree, acts\_as\_nested\_set and various  
other plugins for managing hierarchic data structures.

Isak
