Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

awesome nested set order by

Im using the awesome nested set plugin for ruby on rails. How do I go about sorting by like :name column or something ?

Currently shows the tree like

A
- C
- B

I want it like

A
- B
- C
like image 236
Andrew Cetinic Avatar asked Oct 24 '25 04:10

Andrew Cetinic


1 Answers

Doing this overrides the database sort:

@item.children.except(:order).order("your_sort_column")

Example:

organization.self_and_descendants.to_sql
=> "SELECT `organizations`.* FROM `organizations`  WHERE (`organizations`.`lft` >= 1 AND `organizations`.`lft` < 54) ORDER BY `organizations`.`lft`" 

organization.self_and_descendants.except(:order).order("organization_nm").to_sql
=> "SELECT `organizations`.* FROM `organizations`  WHERE (`organizations`.`lft` >= 1 AND `organizations`.`lft` < 54) ORDER BY organization_nm" 
like image 114
Eric K Avatar answered Oct 26 '25 17:10

Eric K