| Module | SymetrieCom::Acts::NestedSet::ClassMethods |
| In: |
lib/better_nested_set.rb
|
better_nested_set ehances the core nested_set tree functionality provided in ruby_on_rails.
This acts provides Nested Set functionality. Nested Set is a smart way to implement an ordered tree, with the added feature that you can select the children and all of their descendents with a single query. The drawback is that insertion or move need some complex sql queries. But everything is done here by this module!
Nested sets are appropriate each time you want either an orderd tree (menus, commercial categories) or an efficient way of querying big trees (threaded posts).
Methods names are aligned on Tree’s ones as much as possible, to make replacment from one by another easier, except for the creation:
in acts_as_tree:
item.children.create(:name => "child1")
# adds a new item at the "end" of the tree, i.e. with child.left = max(tree.right)+1 child = MyClass.new(:name => "child1") child.save # now move the item to its right place child.move_to_child_of my_item
You can use:
and pass them an id or an object.
Other methods added by this mixin are:
These should not be useful, except if you want to write direct SQL:
recommandations: Don‘t name your left and right columns ‘left’ and ‘right’: these names are reserved on most of dbs. Usage is to name them ‘lft’ and ‘rgt’ for instance.
Configuration options are: