I’m new to RoR, and I’m having trouble sorting a list of categories
using acts_as_tree. Specifically, my list produces something like:
History > Modern > 20th Century
History > United States > 20th Century
Juvenile Fiction > Historical > United States > 20th Century
Juvenile Non-Fiction > History > United States > 20th Century
History > Modern > 21st Century
That’s because I’m sorting by “name,” rather than the full ancestry plus
the name. What I would like to do is produce a list like this:
History > Modern > 20th Century
History > Modern > 21st Century
History > United States > 20th Century
Juvenile Fiction > Historical > United States > 20th Century
Juvenile Non-Fiction > History > United States > 20th Century
For the life of me, I can’t figure out how to sort on the “long name”
(ancestry + name). Everything I try seems to error out.
For background information, my model looks like this:
class Category < ActiveRecord::Base
acts_as_tree
def ancestors_name
if parent
parent.ancestors_name + parent.name + ’ > ’
else
“”
end
end
def long_name
ancestors_name + name
end
end
The pertinent parts of my controller look like this:
class CategoriesController < ApplicationController
def list
@all_categories = Category.find(:all, :order => “name”)
end
And the list.rhtml looks like this:
Listing categories
Name |
---|
<%= link_to ‘New category’, :action => ‘new’ %>
Of course, I would like to do a lot more with this than just the list,
but if I can figure out how to do the list, I should be able to figure
out the rest.
Any help would be most appreciated!