Reversing display order in for?

I am displaying some data on one of my pages by calling a for loop:

for comment in @comments

and then diplaying it:
<%= link_to comment.send(‘comment_subject’), :controller =>‘comments’ ,
:action => ‘show’, :id => comment, :class => ‘comment’ %>

Works great, but it displays things from oldest to newest… Is there an
easy way to get it to reverse the order so that the newest comments are
on the top?

Hi –

On Sat, 8 Jul 2006, Vince W. wrote:

easy way to get it to reverse the order so that the newest comments are
on the top?

for comment in @comments.reverse

:slight_smile:

Depending on how you’re getting the comments in the first place, you
could also do something like:

@comments = Comment.find(:all, :order => “created_at, DESC”)

in your controller.

David


“To fully realize the potential of Rails, it’s crucial that you take
the time to fully understand Ruby–and with “Ruby for Rails” David
has provided just what you need to help you achieve that goal.”
– DAVID HEINEMEIER HANSSON, in the foreword to RUBY FOR RAILS.
Complete foreword & sample chapters at http://www.manning.com/black!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs