acts_as_tree: Sorting by full hierarchy

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!

Larry Lutz wrote:

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.

Try adding nested set capability to your category model. Then get depth-first order by sorting by the "lft" field.