# Menu structure with multiple parents?

**URL:** <https://www.ruby-forum.com/t/menu-structure-with-multiple-parents/191585>\
**Category:** Rails\
**Created:** [July 14, 2010, 9:15pm UTC](https://www.ruby-forum.com/t/menu-structure-with-multiple-parents/191585 "2010-07-14T21:15:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cojones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@cojones](https://www.ruby-forum.com/u/cojones)\
**Post date:** [July 14, 2010, 9:15pm UTC](https://www.ruby-forum.com/t/menu-structure-with-multiple-parents/191585/1 "2010-07-14T21:15:53Z")

</div>

Hello,

my current menu is based on acts\_as\_tree which worked just fine so far  
but now I need some menu items being shown under more than one parent.

Example:  
menu1  
-submenu1  
-submenu2  
menu2  
-submenu1  
-submenu3  
menu3  
-submenu4  
-submenu1

I thought about a habtm on the menu model but don’t think that’s a good  
idea. Does anyone know a proper solution to this?

---

<div class="post-metadata">

**Author:** ![cojones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@cojones](https://www.ruby-forum.com/u/cojones)\
**Post date:** [July 15, 2010, 8:03am UTC](https://www.ruby-forum.com/t/menu-structure-with-multiple-parents/191585/2 "2010-07-15T08:03:59Z")

</div>

What do you guys think of:  
has\_and\_belongs\_to\_many :parents, :class\_name =\> “Menu”,  
:association\_foreign\_key =\> “parent\_id”, :join\_table =\> “menus\_parents”

---

<div class="post-metadata">

**Author:** ![cojones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@cojones](https://www.ruby-forum.com/u/cojones)\
**Post date:** [July 16, 2010, 2:53am UTC](https://www.ruby-forum.com/t/menu-structure-with-multiple-parents/191585/3 "2010-07-16T02:53:23Z")

</div>

On Jul 15, 2:03 am, Heinz S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> What do you guys think of:  
> has\_and\_belongs\_to\_many :parents, :class\_name =\> “Menu”,  
> :association\_foreign\_key =\> “parent\_id”, :join\_table =\> “menus\_parents”

I think it’s astoundingly bad UI to have identical options in more  
than one place - but that’s just my opinion.

Unless your Menu model is exceptionally complicated, is it _really_  
worth the significant boost in both code and SQL complexity just to  
support this (hopefully uncommon) edge case? DRY is great, but there’s  
a point of diminishing (if not negative) returns…

As to the technical side of the question, you’d want to use a real  
join model (with has\_many :through). How else could you keep track of  
the position of the repeated items in each of the parent menus?

–Matt J.

---

<div class="post-metadata">

**Author:** ![cojones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@cojones](https://www.ruby-forum.com/u/cojones)\
**Post date:** [July 16, 2010, 5:19pm UTC](https://www.ruby-forum.com/t/menu-structure-with-multiple-parents/191585/4 "2010-07-16T17:19:13Z")

</div>

You’re right, has\_many :through is probably a better solution but if you  
don’t like that approach what would yours look like?

Thanks!
