ruby-on-railsrubygemsacts-as-tree

acts_as_tree vs ancestry gem for tree menu


I want to implement a tree menu (simple example of tree menu) in a Rails app that I am working on. I am unsure of whether to use acts_as_tree or Ancestry. Its seems that it would be easier to implements a simple tree menu using acts_as_tree, however, Ancestry is more popular and regularly maintained. Does anyone have any thoughts on this?


Solution

  • Use ancestry. It has more powerful query capabilities as it implements the materialized path pattern, as opposed to acts_as_tree that implements adjacency list.
    There are other options too, like nested set, but materialized path is usually the most comprehensive.

    https://communities.bmc.com/communities/docs/DOC-9902

    If you need to sort in preorder at DB level (for example a paginated tree-grid, a preloaded menu that you iterate and indent/dedent according to the depth in tree for displaying) you need to either use a recursive query, or sortable encoding like nested set or nested interval. (That is if sorting in memory is not an option, and it almost never is.)

    https://github.com/collectiveidea/awesome_nested_set
    https://github.com/clyfe/acts_as_nested_interval

    Each has ups and downs. Choose your what fits you.