Ruby on Rails
TreeToTableMappingWish

acts_as_tree

As of Rails 0.9.3(ActiveRecord 1.2.0), acts_as_tree has been available. acts_as_tree decorates an existing class with a many to many relationship with itself.

class Category < ActiveRecord::Base
  acts_as_tree :order => "name"
end

Here’s how to recur through an acts_as_tree tree in one go using recursive partials:

-http://rafb.net/paste/results/FIvCP658.html -
URLs to rafb.net will eventually be lost; please use a more permanent place


Tree to Table Mapping Wish

Mapping tree structures into tables (to store in a database) are a long standing pain for developers. I am using this as a solution and I’d like to see some tree-mapping class/module such as that in Rails.

The author uses a path_id column to map objects relations’ full path, and uses a clever system to remap them once one have to change a object’s parent.

It’s much simpler to implement a threaded comment system (in a blog, for instance) with that maping. One can even get different identation and thresholds with that (e.g., similar to what one can see in Slashdot).

Unfortunatelly, the author uses \MySQL, and have already said doesn’t intend to support other backends. I would like to help with \PostgreSQL, but I’d like to do that inside Rails.

———-

In my case I only needed a mapping based on parent_id, The few times I have to display the entire tree I would just do a find_all and build the tree structure in ruby which would be quicker than recursing the DB.

I’m quite sure this is not a good solution for a threaded forum though :

<pre> class Category < ActiveRecord::Base belongs_to :parent, :class_name => 'Category', :foreign_key => 'parent_id' has_many :children, :class_name => 'Category', :foreign_key => 'parent_id', :dependent => true end

@boards, @boots, @powder, @deepsnow are fixtures structured like this :

boards \_ powder \_ deepsnow boots

testcase:

def test_tree assert_equal true, @boards.has_children? assert_equal false, @boots.has_children? assert_equal boards.children, [powder, @deepsnow] assert_equal @deepsnow.parent, @boards end

However, providing functionality to access Tree like structures is in no way something AR should handle. Thats what addons are for —TobiasLuetke

———-

Why access to tree like structure shouldn’t be handle by AR? rails provides has_one, has_many, belongs_to and has_and_belongs_to_many….why not a has_one_parent_and_has_many_children method for self table referencing???

mapping tree structure into one table is one of the most used feature around the web…every blog or forum that support comments for instance.
It should be supported right of the box by rails (in AR or somewhere else) just because it’s a very frequently used feature. NO?

Hangon

———-

I am reading pablo’s code right now, and I think that a has_one_parent_and_has_many_children method should have the same effect as including his Tree module. Actually, I think that including a module could be much easier, but it surely doesn’t seems rails common speech.

I can help with SQLite code… Although I am still learning it…

Ahh.. you are right TobiasLuetke! Only using parent_id is not quite a good solution for a threaded forum.

Ahh.. you are right TobiasLuetke! Only using parent_id is not quite a good solution for a threaded forum.— Filipe Lang