# Self referential many to many join?

**URL:** <https://www.ruby-forum.com/t/self-referential-many-to-many-join/157997>\
**Category:** Rails\
**Created:** [January 17, 2009, 4:39pm UTC](https://www.ruby-forum.com/t/self-referential-many-to-many-join/157997 "2009-01-17T16:39:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![thepanu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/22d042/32.png) [@thepanu](https://www.ruby-forum.com/u/thepanu)\
**Post date:** [January 17, 2009, 4:39pm UTC](https://www.ruby-forum.com/t/self-referential-many-to-many-join/157997/1 "2009-01-17T16:39:26Z")

</div>

I am trying to create active record schema that includes self  
referential many to many.

Example.

I have models Group and Person. Person can belong to multiple groups and  
that isn’t the problem here. Problem is that I want groups to be able to  
have both groups as well as people as members. And groups can be  
subgroups in several parent groups at the same time (so if I understood  
correctly ActsAsTree won’t work in this situation)

I tried to do it with has\_many, :through as well as with :polymorphic.  
But wasn’t succesfull. So my question is that is this at all possible or  
should I look for workaround to this problem?

Code can be found from pastebin: [http://pastebin.com/f1fc609d3](http://pastebin.com/f1fc609d3)

All suggestions appreciated.

---

<div class="post-metadata">

**Author:** ![thepanu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/22d042/32.png) [@thepanu](https://www.ruby-forum.com/u/thepanu)\
**Post date:** [January 18, 2009, 3:49pm UTC](https://www.ruby-forum.com/t/self-referential-many-to-many-join/157997/2 "2009-01-18T15:49:50Z")

</div>

> I am trying to create active record schema that includes self  
> referential many to many.
> 
> Example.
> 
> I have models Group and Person.

It would be worth your while to check out the schema used in OpenACS  
([http://openacs.org/](http://openacs.org/))

---

<div class="post-metadata">

**Author:** ![thepanu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/22d042/32.png) [@thepanu](https://www.ruby-forum.com/u/thepanu)\
**Post date:** [January 18, 2009, 8:42pm UTC](https://www.ruby-forum.com/t/self-referential-many-to-many-join/157997/3 "2009-01-18T20:42:38Z")

</div>

Thanks for that.

I found a way to achieve this from this blogpost:  
[http://blog.hasmanythrough.com/2007/10/30/self-referential-has-many-through](http://blog.hasmanythrough.com/2007/10/30/self-referential-has-many-through)
