Using awesome_nested_set with Rails 3, I've created a hierarchical categories system. To display the category selector in the view, I've used the following code:
<%= form.select :parent_id, options_for_select(nested_set_options(Category, @category) {|i| "#{'-' * i.level} #{i.name}" }.unshift(["No Parent", nil]), @category.parent_id) %>
I'm attempting to order the categories in alphabetical order, on a level by level basis. If I change the nested_set_options(Category, @category)
to nested_set_options(Category.order("name"), @category)
this will reorder the whole categories list by name; what I want to do is reorder the children of each node alphabetically by name.
For example, I want to resulting select menu to be ordered like this:
Animal
- Bird
-- Chicken
-- Hawk
- Fish
-- Cod
-- Goldfish
-- Trout
- Mammal
-- Cat
-- Primate
--- Chimpanzee
--- Human
-- Zebra
Plant
- Tree