Forum: Ruby on Rails Data structure for "linked list"

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
6fcc5beaeb2e903d6ac5f65f5f3cbb81?d=identicon&s=25 Jack (Guest)
on 2009-04-01 20:04
(Received via mailing list)

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

categories_categories table (not defined as a model; should it be?)

Then I should be able to do things like this...

Fruit -> Berry -> Blueberry
Fruit -> Berry -> Raspberry
Fruit -> Apple -> Rome
Food -> Fruit
Food -> Beverage

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

where object is the other model/table in the many-to-many
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.

6fcc5beaeb2e903d6ac5f65f5f3cbb81?d=identicon&s=25 Jack (Guest)
on 2009-04-01 22:20
(Received via mailing list)
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:


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

This also seems to imply that I do want to make my
categories_categories table a full blown model, with a better name of

Could someone confirm I'm on the right track?

Aad37b5f7116c8d1f547d23b37566032?d=identicon&s=25 Greg Donald (destiney)
on 2009-04-01 23:22
(Received via mailing list)
On Wed, Apr 1, 2009 at 1:03 PM, Jack <> wrote:
> I'm trying to figure out the best approach to creating a linked list
> conceptually in RoR.

Greg Donald
Dd2d775dea75b381edb1bbf0600a0907?d=identicon&s=25 Marnen Laibow-Koser (marnen)
on 2009-04-02 06:03
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.

Marnen Laibow-Koser
This topic is locked and can not be replied to.