# Many to Many Category structure with itself

**URL:** <https://www.ruby-forum.com/t/many-to-many-category-structure-with-itself/58320>\
**Category:** Rails\
**Created:** [April 20, 2006, 4:34pm UTC](https://www.ruby-forum.com/t/many-to-many-category-structure-with-itself/58320 "2006-04-20T16:34:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kyle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/848f3c/32.png) [@kyle](https://www.ruby-forum.com/u/kyle)\
**Post date:** [April 20, 2006, 4:34pm UTC](https://www.ruby-forum.com/t/many-to-many-category-structure-with-itself/58320/1 "2006-04-20T16:34:50Z")

</div>

Basically, I’m trying to have a table called “categories” have a many to  
many relationship with itself. But I also want each catagory to be able  
to be “copied” into another category so it is essentially a child  
category to more than one parent. To me the obvious way of doing this is  
by creating another table called category\_maps (and a model called  
CategoryMap). The category\_maps table would keep the parent\_id/child\_id  
relationships for the categories table.  
As far as the user experience, I want it to work so if you go to  
controllername/list\_cats (with no id) it will display all cats with a  
parent\_id of 0. If they go to controllername/list\_cats/3 it should  
display all cats with a parent\_id of 3  
My problem is that when I try to implement this concept in the actuall  
code, I run into problem after problem so I must be missing something.  
If anyone can help or at least send me to an OS app that has the code I  
need, I would greatly appreciate it.

---

<div class="post-metadata">

**Author:** ![kyle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/848f3c/32.png) [@kyle](https://www.ruby-forum.com/u/kyle)\
**Post date:** [April 20, 2006, 5:45pm UTC](https://www.ruby-forum.com/t/many-to-many-category-structure-with-itself/58320/2 "2006-04-20T17:45:50Z")

</div>

Kyle wrote:

> Basically, I’m trying to have a table called “categories” have a many to  
> many relationship with itself. But I also want each catagory to be able  
> to be “copied” into another category so it is essentially a child  
> category to more than one parent. To me the obvious way of doing this is  
> by creating another table called category\_maps (and a model called  
> CategoryMap). The category\_maps table would keep the parent\_id/child\_id  
> relationships for the categories table.  
> As far as the user experience, I want it to work so if you go to  
> controllername/list\_cats (with no id) it will display all cats with a  
> parent\_id of 0. If they go to controllername/list\_cats/3 it should  
> display all cats with a parent\_id of 3  
> My problem is that when I try to implement this concept in the actuall  
> code, I run into problem after problem so I must be missing something.  
> If anyone can help or at least send me to an OS app that has the code I  
> need, I would greatly appreciate it.

You can do what you describe using a self-referential  
has\_and\_belongs\_to\_many association. Check on the Rails wiki for some  
examples. You _should_ be able to do this with a has\_many :through, but  
there’s a bug that prevents it from working (which I’m trying to get  
fixed).

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![kyle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/848f3c/32.png) [@kyle](https://www.ruby-forum.com/u/kyle)\
**Post date:** [April 20, 2006, 5:55pm UTC](https://www.ruby-forum.com/t/many-to-many-category-structure-with-itself/58320/3 "2006-04-20T17:55:14Z")

</div>

Also see acts\_as\_tree it might be better.

---

<div class="post-metadata">

**Author:** ![kyle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/848f3c/32.png) [@kyle](https://www.ruby-forum.com/u/kyle)\
**Post date:** [April 21, 2006, 8:20pm UTC](https://www.ruby-forum.com/t/many-to-many-category-structure-with-itself/58320/4 "2006-04-21T20:20:18Z")

</div>

Kris wrote:

> Also see acts\_as\_tree it might be better.

I worked everything out. the acts\_as\_tree method is very helpfull. Thank  
you

---

<div class="post-metadata">

**Author:** ![kyle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/848f3c/32.png) [@kyle](https://www.ruby-forum.com/u/kyle)\
**Post date:** [April 21, 2006, 9:19pm UTC](https://www.ruby-forum.com/t/many-to-many-category-structure-with-itself/58320/5 "2006-04-21T21:19:25Z")

</div>

Kyle wrote:

> Kris wrote:
> 
> > Also see acts\_as\_tree it might be better.
> 
> I worked everything out. the acts\_as\_tree method is very helpfull. Thank  
> you

I got the self-ref has\_many :through worked out:

[http://blog.hasmanythrough.com/articles/2006/04/21/self-referential-through](http://blog.hasmanythrough.com/articles/2006/04/21/self-referential-through)

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)
