# Newbie seeks helps ordering ancestors from acts\_as\_tree

**URL:** <https://www.ruby-forum.com/t/newbie-seeks-helps-ordering-ancestors-from-acts-as-tree/71975>\
**Category:** Rails\
**Created:** [February 22, 2006, 7:21pm UTC](https://www.ruby-forum.com/t/newbie-seeks-helps-ordering-ancestors-from-acts-as-tree/71975 "2006-02-22T19:21:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_T](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a8b319/32.png) [@Chris\_T](https://www.ruby-forum.com/u/Chris_T)\
**Post date:** [February 22, 2006, 7:21pm UTC](https://www.ruby-forum.com/t/newbie-seeks-helps-ordering-ancestors-from-acts-as-tree/71975/1 "2006-02-22T19:21:44Z")

</div>

I’m sure this is really obvious but I’m feeling my way here with rails  
(and Ruby) and have tried a couple of things without success. So, can  
anyone tell me the best way to reverse order the retuen list of  
ancestors.  
In the controller I have:  
@ancestors = @category.ancestors  
which returns an array starting at the parent and ending with the root.

When I then loop through the array to create a breadcrumb list:  
\<% @ancestors.each do |ancestor| %\>  
\<%= link\_to h(ancestor.name), :action =\>“show”, :id =\> ancestor.id %\> \>  
\<% end %\>

I get the breadcrumb list but in the wrong order:  
parent \> grandparent \> root

Should I be changing the order in the controller or the loop. If so, how  
(zero Ruby skills, as you can prob tell – pickaxe book on order!)  
Thanks  
Chris T

---

<div class="post-metadata">

**Author:** ![Chris\_T](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a8b319/32.png) [@Chris\_T](https://www.ruby-forum.com/u/Chris_T)\
**Post date:** [February 22, 2006, 8:43pm UTC](https://www.ruby-forum.com/t/newbie-seeks-helps-ordering-ancestors-from-acts-as-tree/71975/2 "2006-02-22T20:43:18Z")

</div>

> When I then loop through the array to create a breadcrumb list:  
> \<% @ancestors.each do |ancestor| %\>  
> \<%= link\_to h(ancestor.name), :action =\>“show”, :id =\> ancestor.id %\> \>  
> \<% end %\>
> 
> I get the breadcrumb list but in the wrong order:  
> parent \> grandparent \> root

Try using the Array method reverse or reverse!. I’m pretty new myself  
but I think you can string your methods together and do

\<% @ancestor.reverse.each do |ancestor| %\>

Check out the online Ruby documenation, [http://corelib.rubyonrails.org/](http://corelib.rubyonrails.org/).  
I use them alot.

William

---

<div class="post-metadata">

**Author:** ![Chris\_T](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a8b319/32.png) [@Chris\_T](https://www.ruby-forum.com/u/Chris_T)\
**Post date:** [February 22, 2006, 8:58pm UTC](https://www.ruby-forum.com/t/newbie-seeks-helps-ordering-ancestors-from-acts-as-tree/71975/3 "2006-02-22T20:58:20Z")

</div>

William LeFevre wrote:

> > When I then loop through the array to create a breadcrumb list:  
> > \<% @ancestors.each do |ancestor| %\>  
> > \<%= link\_to h(ancestor.name), :action =\>“show”, :id =\> ancestor.id %\> \>  
> > \<% end %\>
> > 
> > I get the breadcrumb list but in the wrong order:  
> > parent \> grandparent \> root
> 
> Try using the Array method reverse or reverse!. I’m pretty new myself  
> but I think you can string your methods together and do
> 
> \<% @ancestor.reverse.each do |ancestor| %\>
> 
> Check out the online Ruby documenation, [http://corelib.rubyonrails.org/](http://corelib.rubyonrails.org/).  
> I use them alot.
> 
> William  
> Doh! Yup that worked great. Obviously didn’t look properly in the ruby  
> docs. Many thanks!  
> Chris
