3

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?

Tom Kadwill
  • 1,448
  • 3
  • 15
  • 21
  • Why would it be simpler with acts_as_tree? I'd recommend you use ancestry as there's only one db field on the model. You might be interested in this http://railscasts.com/episodes/262-trees-with-ancestry – mark Jan 12 '12 at 12:58
  • @mark you are right. However, I tried to follow the railscast that you mention but I am having problems with [rake db:migrate](http://stackoverflow.com/questions/8828204/ancestry-dbmigrate) – Tom Kadwill Jan 12 '12 at 13:58
  • Probably have to ask another question for that but try bundle exec rake db:migrate – mark Jan 12 '12 at 14:11
  • @mark thanks for the advice, unfortunately bundle exec rake db:migrate fails as well. I think I may have to create more than just the migration file and the model file, I'll look into it some more – Tom Kadwill Jan 12 '12 at 18:47

1 Answers1

11

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.

clyfe
  • 23,695
  • 8
  • 85
  • 109