# Data structure for "linked list"

**URL:** <https://www.ruby-forum.com/t/data-structure-for-linked-list/164476>\
**Category:** Rails\
**Created:** [April 1, 2009, 8:04pm UTC](https://www.ruby-forum.com/t/data-structure-for-linked-list/164476 "2009-04-01T20:04:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cc9497/32.png) [@jack](https://www.ruby-forum.com/u/jack)\
**Post date:** [April 1, 2009, 8:04pm UTC](https://www.ruby-forum.com/t/data-structure-for-linked-list/164476/1 "2009-04-01T20:04:06Z")

</div>

Folks,

I’m trying to figure out the best approach to creating a linked list  
conceptually in RoR. This is what I’m looking at doing and want to  
know if there is a best practice approach to this already (my search  
didn’t seem to indicate this) OR if I’m completely off base on the  
naming convention I’m using:

category table  
id  
name

categories\_categories table (not defined as a model; should it be?)  
id  
category\_id  
parent\_category\_id

Then I should be able to do things like this…

Fruit -\> Berry -\> Blueberry  
Fruit -\> Berry -\> Raspberry  
Fruit -\> Apple -\> Rome  
Food -\> Fruit  
Food -\> Beverage  
etc.

where I create these by recursively(?) accessing the  
categories\_categories data joined to the category data. The start of a  
category will be implied where parent\_category\_id is 0. And those  
parent records could be a foreign key like so

categories\_categories\_object table  
object\_id  
categories\_categories\_id

where object is the other model/table in the many-to-many  
relationship.  
Is this the appropriate way to do something like this? If not, could  
someone point me to some documentation or an example of how I should  
be handling this.

Thanks,  
Jack

---

<div class="post-metadata">

**Author:** ![jack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cc9497/32.png) [@jack](https://www.ruby-forum.com/u/jack)\
**Post date:** [April 1, 2009, 10:20pm UTC](https://www.ruby-forum.com/t/data-structure-for-linked-list/164476/2 "2009-04-01T22:20:21Z")

</div>

I just found some info in the Agile Rails book and with a bit more  
searching I discovered I could look into one of these:

acts\_as\_list  
acts\_as\_tree  
acts\_as\_nested

I don’t think the list feature works since I’m looking at a hierarchy  
and want to be able to re-use the same term in different levels.

Can acts\_as\_tree allow for multiple trees? i.e. more than one NULL  
parent\_id?

This also seems to imply that I do want to make my  
categories\_categories table a full blown model, with a better name of  
course.

Could someone confirm I’m on the right track?

Thanks,  
Jack

---

<div class="post-metadata">

**Author:** ![jack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cc9497/32.png) [@jack](https://www.ruby-forum.com/u/jack)\
**Post date:** [April 1, 2009, 11:22pm UTC](https://www.ruby-forum.com/t/data-structure-for-linked-list/164476/3 "2009-04-01T23:22:13Z")

</div>

On Wed, Apr 1, 2009 at 1:03 PM, Jack [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m trying to figure out the best approach to creating a linked list  
> conceptually in RoR.

[http://www.koders.com/ruby/fidCB5A463AB556F4FFD20E03139FF59563B118D108.aspx?s=socket](http://www.koders.com/ruby/fidCB5A463AB556F4FFD20E03139FF59563B118D108.aspx?s=socket)

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)

---

<div class="post-metadata">

**Author:** ![jack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cc9497/32.png) [@jack](https://www.ruby-forum.com/u/jack)\
**Post date:** [April 2, 2009, 6:03am UTC](https://www.ruby-forum.com/t/data-structure-for-linked-list/164476/4 "2009-04-02T06:03:48Z")

</div>

Jack wrote:

> Folks,
> 
> I’m trying to figure out the best approach to creating a linked list  
> conceptually in RoR.  
> […]

What you describe is a tree, not really a linked list. You could use  
acts\_as\_tree for this, but you’ll probably get better results with  
awesome\_nested\_set or something similar.

## Best,

Marnen Laibow-Koser  
removed\_email\_address@domain.invalid  
[http://www.marnen.org](http://www.marnen.org)
