# Many:many relation on a single table

**URL:** <https://www.ruby-forum.com/t/many-many-relation-on-a-single-table/91834>\
**Category:** Rails\
**Created:** [April 12, 2007, 7:21pm UTC](https://www.ruby-forum.com/t/many-many-relation-on-a-single-table/91834 "2007-04-12T19:21:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [April 12, 2007, 7:21pm UTC](https://www.ruby-forum.com/t/many-many-relation-on-a-single-table/91834/1 "2007-04-12T19:21:48Z")

</div>

Consider a tree. Not a perfect example since every branch only has  
one parent…but you get the idea.

What I want is the following:

Branch.children  
Branch.parents

I think the problem is not being able to change the method name  
without changing the attribute it points to. Here is my idea so far.

Thanx for looking!

class Branch \< ActiveRecord::Base  
has\_many :parents, :through =\> :relations  
has\_many :children, :through =\> :relations  
end

class Relation \< ActiveRecord::Base  
belongs\_to :branch, :foreign\_key =\> “parent\_id”  
belongs\_to :branch, :foreign\_key =\> “child\_id”  
end

---------DB-------  
table Relations  
parent\_id int  
child\_id int  
foo —  
bar —

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [April 12, 2007, 7:31pm UTC](https://www.ruby-forum.com/t/many-many-relation-on-a-single-table/91834/2 "2007-04-12T19:31:49Z")

</div>

You’re going to have to be very, very careful to not create loops (eg  
situations where something is a child of itself) if you’re going after a  
tree structure. That’s the sort of data integrity thing that I would  
shove  
right down to the database layer as triggers on your appropriate table.

The schema here (from my old perl days) does that:

[http://search.cpan.org/src/CRAKRJACK/Schema-RDBMS-AUS-0.03/schema/](http://search.cpan.org/src/CRAKRJACK/Schema-RDBMS-AUS-0.03/schema/)

In postgres:

[http://search.cpan.org/src/CRAKRJACK/Schema-RDBMS-AUS-0.03/schema/Pg/0.01/216\_user\_membership\_insert.sql](http://search.cpan.org/src/CRAKRJACK/Schema-RDBMS-AUS-0.03/schema/Pg/0.01/216_user_membership_insert.sql)

In MySQL:

[http://search.cpan.org/src/CRAKRJACK/Schema-RDBMS-AUS-0.03/schema/mysql/0.01/216\_user\_membership\_insert.sql](http://search.cpan.org/src/CRAKRJACK/Schema-RDBMS-AUS-0.03/schema/mysql/0.01/216_user_membership_insert.sql)

Hopefully you’ll find something there you can use.

Cheers,  
Tyler

Josh [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> without changing the attribute it points to. Here is my idea so far.  
> belongs\_to :branch, :foreign\_key =\> “child\_id”
> 
> >

–

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [April 12, 2007, 7:32pm UTC](https://www.ruby-forum.com/t/many-many-relation-on-a-single-table/91834/3 "2007-04-12T19:32:49Z")

</div>

Hmm, check out plugins like acts\_as\_graph:  
[http://blog.tammersaleh.com/pages/acts\_as\_graph](http://blog.tammersaleh.com/pages/acts_as_graph)

Jason
