closure_tree icon indicating copy to clipboard operation
closure_tree copied to clipboard

hash_tree doesn't eager load children

Open villesundberg opened this issue 10 years ago • 1 comments

The current implementation of hash_tree doesn't connect the returned objects together.

1 node = Node.find_by_id(1)
2 tree = node.hash_tree
3 tree.keys.first.children

Line 3 above will hit the database again, even though the children of node 1 have already been retrieved. It would be reasonable to expect line 2 to construct the tree so that calls to children or parent don't hit the database.

Is this by design, or would this be a desirable improvement?

villesundberg avatar May 28 '15 10:05 villesundberg

An alternative (which would be useful) would be to have a hash_tree-like method that just returned the primary key IDs of the records in the tree, not the full records. This could then be used to pluck the actual items out of a collection that has been hashed by ID (e.g. using .index_by(&:id) on the end of an AR relation).

transoceanic2000 avatar Jan 13 '16 21:01 transoceanic2000