Newbie seeks helps ordering ancestors from acts_as_tree


#1

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


#2

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/.
I use them alot.

William


#3

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/.
I use them alot.

William
Doh! Yup that worked great. Obviously didn’t look properly in the ruby
docs. Many thanks!
Chris