views:

164

answers:

1

Using acts_as_tree I would like to be able to preload an entire tree having its complete child hierarchy intact with one SQL call. To that end, I added a tree_id to the table and it runs through all descendants in that tree.

I had explored acts_as_nested_set (actually awesome_nested_set) as a possibility but since I graft trees into other trees, I found that using the nested set for my purposes performed far too many updates. Along with acts_as_versioned this is an unacceptable complication to the design I'm after. For my purposes, I believe acts_as_tree is better suited.

My only issue is to grab a whole tree with the hierarchy intact. The :include option of ActiveRecord works with :children but not :descendants. I am content with writing my own method for retrieving and mapping the associations manually. Any guidance or examples for how to accomplish this?

From my point of view, the only benefit of nested sets that I'm putting aside to use tree (one that supports grabbing the entire structured tree) is the selective grabbing any subsection of a tree. I'm okay with that.

The solution I'm hoping to avoid is to eliminate the :children association that comes along with tree and defining and manually loading a children array defined on each tree node.

A: 

I've looked into this in the past and IIRC I found that you can load a tree of a known depth with a single SQL query by joining the table with itself n times; however, it's not possible to load a complete tree of arbitrary size. Hence the need for the nested set design.

If your data set is relatively small, you could fetch all the records and re-assemble the tree(s) in memory. Perhaps that would suffice?

Luke Francl