Complex/nested relationships

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

Take a look at the acts_as_list plugin - I think it will solve your problem with the List model.

-Dan Manges

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!

Oops. I meant to suggest acts_as_tree. Your solution sounds good.

Dan

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