Ordering Lists of Objects

Once you’ve created a model class, you may want to retrieve the list of the top 10 newest objects. How can you do that?

The answer lies in the find function (similar to find_all). It can take some additional parameters–notably, an order clause, and a limit.

So say you have an object called Article, and you want the most recent 10 articles. You can simply add this code to your controller:

@newestTen = Article.find :all, :order => "updated_on desc", :limit => 10

…and you get the ten most recent articles!

For more details on find, you can find (pun not intended) a reference here.

About Ashiq Alibhai

Ashiq Alibhai, PMP, has been a Rails aficionado since 2007, and developed web applications since early 2003, where he learned PHP in one summer. As the driving-force behind RailsRocket and the Launchpad project, he seeks to share the ease of development with Rails far and wide.
This entry was posted in Development and tagged , . Bookmark the permalink.

Comments are closed.