# Complex/nested relationships

**URL:** <https://www.ruby-forum.com/t/complex-nested-relationships/141621>\
**Category:** Rails\
**Created:** [June 27, 2008, 8:47pm UTC](https://www.ruby-forum.com/t/complex-nested-relationships/141621 "2008-06-27T20:47:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bmcelhany](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a88e57/32.png) [@bmcelhany](https://www.ruby-forum.com/u/bmcelhany)\
**Post date:** [June 27, 2008, 8:47pm UTC](https://www.ruby-forum.com/t/complex-nested-relationships/141621/1 "2008-06-27T20:47:32Z")

</div>

Hello, I’m relatively new to Rails and had a question about how to  
best set up relationships between a few of my objects. Right now, I’ve  
got the following model objects:

Transmission  
List  
Technique

A Transmission contains many Lists and a List contains many  
Techniques. So far so good. However, a List can also contain many  
other Lists (a kind of “sub list”) and these “sub lists” will also  
contain many Techniques.

So, the relation between Transmissions and Lists is pretty  
straightforward, as is the relationship between Techniques and Lists.  
The problem I’m having is in regards to this recursive relationship  
between Lists and their child Lists. A list could contain:

1. Only techniques (i.e. no other Lists)
2. A combination of Techniques and Lists (each List with its own  
Techniques)
3. Only child Lists (i.e. no Techniques as direct children)

I’m not sure if it makes things easier or not, but a given List will  
never be a parent to other Lists AND a child of another List…only  
one or the other. In other words, if ListA has a child, ListB, then  
ListB will never really exist outside of the context of ListA.

After writing this out, it’s even more confusing than before. I hope  
someone can point me in the right direction with this. Thanks in  
advance!

-Brian

---

<div class="post-metadata">

**Author:** ![bmcelhany](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a88e57/32.png) [@bmcelhany](https://www.ruby-forum.com/u/bmcelhany)\
**Post date:** [June 28, 2008, 3:46pm UTC](https://www.ruby-forum.com/t/complex-nested-relationships/141621/2 "2008-06-28T15:46:39Z")

</div>

Take a look at the acts\_as\_list plugin - I think it will solve your  
problem with the List model.

-Dan M.

---

<div class="post-metadata">

**Author:** ![bmcelhany](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a88e57/32.png) [@bmcelhany](https://www.ruby-forum.com/u/bmcelhany)\
**Post date:** [June 28, 2008, 8:56pm UTC](https://www.ruby-forum.com/t/complex-nested-relationships/141621/3 "2008-06-28T20:56:22Z")

</div>

Thanks for the suggestion, Dan. I’ve used acts\_as\_list a bit in  
another project…I was under the impression that it’s primary purpose  
was to add ordering functionality to an object.

I guess the bottom line is that, when I add (or edit) one of my List  
objects, I need to be able to specify that it is a child of either a  
Transmission object (effectively making it a top-level List) or of  
another List object (effectively making it a child List). It would  
never be a child of both…only one or the other.

They way I’m thinking of doing it is having two columns in my table:  
transmission\_id and parent\_id. If the List object I’m adding “belongs”  
to a Transmission, then the transmission\_id field would be populated  
and the parent\_id field would be null. If the List object is a child  
list of a parent List object, then the parent\_id would have a value  
and the transmission\_id field would be null.

Doesn’t seem like the cleanest solution, but I think it may work. Is  
there a better way to do this?

Thanks!

---

<div class="post-metadata">

**Author:** ![bmcelhany](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a88e57/32.png) [@bmcelhany](https://www.ruby-forum.com/u/bmcelhany)\
**Post date:** [June 29, 2008, 5:00pm UTC](https://www.ruby-forum.com/t/complex-nested-relationships/141621/4 "2008-06-29T17:00:50Z")

</div>

Brian,  
If you ever get your complex ‘nested’ relationships to work, I’d love  
to hear how. I recall years ago when I worked in manufacturing bill of  
materials (BOM’s) we accomplished the same thing by setting a ‘nesting  
level’ and using this to calculate the product tree. If you ever got  
it WRONG the stack overflows were scary and we’d have to turn off the  
machine.  
Kathleen

---

<div class="post-metadata">

**Author:** ![bmcelhany](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a88e57/32.png) [@bmcelhany](https://www.ruby-forum.com/u/bmcelhany)\
**Post date:** [June 28, 2008, 11:52pm UTC](https://www.ruby-forum.com/t/complex-nested-relationships/141621/5 "2008-06-28T23:52:51Z")

</div>

Oops. I meant to suggest acts\_as\_tree. Your solution sounds good.

Dan
