# Associations in the same table

**URL:** <https://www.ruby-forum.com/t/associations-in-the-same-table/110525>\
**Category:** Rails\
**Created:** [July 27, 2007, 5:20am UTC](https://www.ruby-forum.com/t/associations-in-the-same-table/110525 "2007-07-27T05:20:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![snacktime](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7feea3/32.png) [@snacktime](https://www.ruby-forum.com/u/snacktime)\
**Post date:** [July 27, 2007, 5:20am UTC](https://www.ruby-forum.com/t/associations-in-the-same-table/110525/1 "2007-07-27T05:20:06Z")

</div>

I’m trying to figure out what is the ‘rails’ way of doing this. I’m  
expressing products and their variations. Variations are just  
products that belong to a parent product. It’s basically a tree  
structure. Variations can also belong to other variations. What I’m  
trying to decide is the best way to get all the descendents for a  
product in one query and keep the table as normalized as possible.  
The un-normalized way would be to add a root\_id column, or I could use  
a join table I guess. Any thoughts?

Chris

---

<div class="post-metadata">

**Author:** ![snacktime](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7feea3/32.png) [@snacktime](https://www.ruby-forum.com/u/snacktime)\
**Post date:** [July 27, 2007, 5:41am UTC](https://www.ruby-forum.com/t/associations-in-the-same-table/110525/2 "2007-07-27T05:41:07Z")

</div>

Check out acts\_as\_nested\_set, haven’t used it myself, but apparently  
it’s really nice for this sort of stuff, allowing you to get  
everything in one query.

/Jonas

---

<div class="post-metadata">

**Author:** ![snacktime](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7feea3/32.png) [@snacktime](https://www.ruby-forum.com/u/snacktime)\
**Post date:** [July 27, 2007, 5:58am UTC](https://www.ruby-forum.com/t/associations-in-the-same-table/110525/3 "2007-07-27T05:58:08Z")

</div>

On 7/26/07, JN\_Coward [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Check out acts\_as\_nested\_set, haven’t used it myself, but apparently  
> it’s really nice for this sort of stuff, allowing you to get  
> everything in one query.

I saw that. I was a little concerned about the added complexity of  
removing or moving sets to different locations in the tree. I should  
play with it a little though to see how well it works in practice.

Chris
