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:
<h1>Listing categories</h1>
<table>
<tr>
<th>Name</th>
</tr>
<% for category in @all_categories %>
<tr>
<td><%=h category.long_name %></td>
<td><%= link_to ''Edit'', :action =>
''edit'', :id => category %></td>
<td><%= link_to ''Destroy'', { :action =>
''destroy'', :id => category
}, :confirm => ''Are you sure?'', :method => :post
%></td>
</tr>
<% end %>
</table>
<br />
<%= 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!
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---