Forum: Ruby on Rails Newbie seeks helps ordering ancestors from acts_as_tree

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
925c5035ff3e0005e0604c0cd869b5cf?d=identicon&s=25 Chris T (Guest)
on 2006-02-22 19:21
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
C56f6508ce4610e8527606a435b5870f?d=identicon&s=25 William Lefevre (wlefevre)
on 2006-02-22 20:43
> 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
925c5035ff3e0005e0604c0cd869b5cf?d=identicon&s=25 Chris T (Guest)
on 2006-02-22 20:58
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
This topic is locked and can not be replied to.