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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community 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)
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
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:

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
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 <jlk4p@virginia.edu> wrote:
> I'm trying to figure out the best approach to creating a linked list
> conceptually in RoR.

http://www.koders.com/ruby/fidCB5A463AB556F4FFD20E...



--
Greg Donald
http://destiney.com/
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.

Best,
--
Marnen Laibow-Koser
marnen@marnen.org
http://www.marnen.org
This topic is locked and can not be replied to.