# Acts\_as\_tree: Sorting by full hierarchy

**URL:** <https://www.ruby-forum.com/t/acts-as-tree-sorting-by-full-hierarchy/128686>\
**Category:** Rails\
**Created:** [February 10, 2008, 2:45pm UTC](https://www.ruby-forum.com/t/acts-as-tree-sorting-by-full-hierarchy/128686 "2008-02-10T14:45:14Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![larrytx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f475e1/32.png) [@larrytx](https://www.ruby-forum.com/u/larrytx)\
**Post date:** [February 10, 2008, 2:45pm UTC](https://www.ruby-forum.com/t/acts-as-tree-sorting-by-full-hierarchy/128686/1 "2008-02-10T14:45:14Z")

</div>

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

```
<% 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 %\>

 \<% end %\>

| 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!

---

<div class="post-metadata">

**Author:** ![larrytx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f475e1/32.png) [@larrytx](https://www.ruby-forum.com/u/larrytx)\
**Post date:** [February 14, 2008, 3:50pm UTC](https://www.ruby-forum.com/t/acts-as-tree-sorting-by-full-hierarchy/128686/2 "2008-02-14T15:50:36Z")

</div>

Larry Lutz wrote:

> 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.

–  
We develop, watch us RoR, in numbers too big to ignore.
