Examples

Continue to declare your model as usual:

acts_as_nested_set

Root of the tree is the item where parent_id is null. You can have multiple roots (for threads in mails lists, or base items of menus). First item always has a left value of 1.

New item is added to the end of the tree: new_left = max(existing_right). So you need one more operation than wiht acts_as_tree to add an item:

m = MyModel.new(params)
# save to have left and right
m.save
# now move to the right place
m.move_to_child_of Model.find(12)

add_child is deprecated (to my taste), I'll remove it soon.

New methods are:

  • parent
  • ancestors
  • self_and_ancestors
  • siblings
  • self_and_siblings
  • level (root is level 0)
  • children_count
  • full_set
  • all_children (full_set - self)
  • children
  • move_to_left_of
  • move_to_right_of
  • move_to_child_of