0

In default AwesomeNestedSet gem is sorting by :lft attribute. Suppose I have a class:

class Category < ActiveRecord::Base
 acts_as_nested_set
 attr_accessible :name, :position, :parent_id, :lft, :rgt
end

How can I create a sortable (by :position attribute) tree with AwesomeNestedSet gem with one hit to the database where :position is used for sorting siblings (level)?

I need output something like this:

 
----------------------------------
id |position | name   | parent_id |
----------------------------------
1  | 1       | item1  | nil       |
----------------------------------
2  | 1       | item11 | 1         |
----------------------------------
3  | 1       | item111| 2         |
----------------------------------
4  | 2       | item12 | 1         |
----------------------------------
5  | 2       | item2  | nil       |
----------------------------------
Alexander Shlenchack
  • 3,779
  • 6
  • 32
  • 46
  • Can you elaborate a little on what you mean by "sorting siblings"? – Ruby Racer Mar 23 '14 at 18:39
  • In my understanding, what you try to achieve is not a tree. To have a tree, you need a specific structure, with roots, nodes, leafs, ancestors, descendants, parents, children. How does "position" describe any of these relations? What you are planning is a sort operation and not a tree. If you need to sort first by position and then by tree structure, then that is what you have to define, but not as a tree. Unless, if by some wild chance you mean "how far to the left is the node from rightmost level peer". Is this what you mean? – Ruby Racer Mar 23 '14 at 19:29
  • As you can see from a required output it's sorted tree not list. Every level sorted by :position. – Alexander Shlenchack Mar 23 '14 at 19:57
  • Ok, I think I got this. One last question to make sure. Is position the "horizontal level", automatically calculated, or is it a value you provide by hand? – Ruby Racer Mar 23 '14 at 21:31
  • It is provided by hand – Alexander Shlenchack Mar 23 '14 at 21:52

0 Answers0