Has anyone extended Rails’s acts_as_tree to use the Modified Preorder
Traversal algorithm? This is a really clever trick for optimizing
access to trees stored in databases, as described lucidly in this
It adds two integer fields, a “left counter” and “right counter”, to
each row. By updating these in the right way as rows are inserted and
deleted in the tree, you can use them to reduce a lot of common tree
operations – like getting the path to a node or finding all the
descendends of a node – to a single query.
I used this in the original PHP implementation of my app. I’m sure it
can be done easily in Rails, but I’m not comfortable yet doing direct
SQL munging on ActiveRecord, so I’d appreciate any tips or pointers
to existing code.