# Self referential Models

**URL:** <https://www.ruby-forum.com/t/self-referential-models/112505>\
**Category:** Rails\
**Created:** [August 20, 2007, 12:01pm UTC](https://www.ruby-forum.com/t/self-referential-models/112505 "2007-08-20T12:01:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![daz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@daz](https://www.ruby-forum.com/u/daz)\
**Post date:** [August 20, 2007, 12:01pm UTC](https://www.ruby-forum.com/t/self-referential-models/112505/1 "2007-08-20T12:01:18Z")

</div>

If I have a Person model, and every person has a parent, that is in  
fact another person, can I still use the belongs\_to and has\_many  
methods?

A simple database migration might look like this

```
  t.column :name, :string
  t.column :parent, :integer

```

So every person only has a name and some people have parents.

I have written some methods that find a person’s parent and children:

class Person \< ActiveRecord::Base  
def find\_children  
Person.find\_all\_by\_parent(self.id)  
end

def find\_parent  
Person.find\_by\_id(self.parent)  
end  
end

These seem to work fine, but I was wondering if there was a neater way  
to do this?

Thanks,

DAZ

---

<div class="post-metadata">

**Author:** ![daz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@daz](https://www.ruby-forum.com/u/daz)\
**Post date:** [August 20, 2007, 2:37pm UTC](https://www.ruby-forum.com/t/self-referential-models/112505/2 "2007-08-20T14:37:26Z")

</div>

if you don’t need multiple parents per child (i.e, a parent has a lot of  
kids, and each kid has one parent) then this should be perfect for you:

[http://api.rubyonrails.com/classes/ActiveRecord/Acts/Tree/ClassMethods.html#M000662](http://api.rubyonrails.com/classes/ActiveRecord/Acts/Tree/ClassMethods.html#M000662)

no need for two tables; it is self-referential in the sense that it  
points back to the same table (=model) via a parent\_id column.

some other good pointers while you’re at it:

acts\_as\_tree (the above link)  
acts\_as\_nested\_set (for getting a whole bunch of kids in one query,  
instead of recursive calls)

this is a real self-referential model usage;  
[has\_many :through - Self-referential has\_many :through associations](http://blog.hasmanythrough.com/2006/4/21/self-referential-through) :

just general knowledge, the best place to use this is for friendships  
(where X can be a friend of Y, without Y being a friend of X, as they  
are all “friends”)

anyway… i hope the above helps.  
good luck digging 🙂

---

<div class="post-metadata">

**Author:** ![daz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@daz](https://www.ruby-forum.com/u/daz)\
**Post date:** [August 21, 2007, 11:04pm UTC](https://www.ruby-forum.com/t/self-referential-models/112505/3 "2007-08-21T23:04:55Z")

</div>

Thanks for this Shai,

acts\_as\_tree is exactly what I was looking for. I might also use  
acts\_as\_list also.

Thanks for the other links too … I’m sure they will make some  
interesting and useful reading.

DAZ
