Alexander Shlenchack
Alexander Shlenchack

Reputation: 3869

AwesomeNestedSet and sortable tree

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       |
----------------------------------

Upvotes: 0

Views: 515

Answers (0)

Related Questions