Topic: reversing list view order?

I have a simple list view (based on the automatic code generated from "generate scaffold").

Currently, when I create a new item, it appears at the bottom of the list, and the whole list displays with the oldest first. How do I reverse this order so the newest item appears on top; oldest at the bottom?

Last edited by dsinker (2006-11-10 17:00:15)

Re: reversing list view order?

You can simply reverse the items array:

<% for item in @items.reverse %>

However, this isn't guaranteed to sort newest to oldest. Instead you should add a created_at datetime column to the items table and order by that when you do the find:

@items = Item.find(:all, :order => 'created_at DESC')

Railscasts - Free Ruby on Rails Screencasts