C S Shyam Sundar's Weblog

the rails way of ordering models

Posted by: Shyam Sundar on: August 5, 2008

Being a newbie to rails, I find many interesting things to do a certain operation. They call it the ruby or the rails way of doing things.

The Fine Print:

The find method, supports passing o a parameter to order based on table columns in ascending or descending order.

Example:

@users = User.find(:all)

Here @users, will have all the users in user table ordered by id.

Suppose, if we want to order by ID in reverse order., the statement becomes.,

@users = User.find(:all, :o rder => “id DESC”)

Suppose, if we want to order by AGE in reverse order, the statement becomes.,

@users = User.find(:all, :o rder => “age DESC”)

Thats it folks!

Tags: , ,

Leave a Reply

You must be logged in to post a comment.