How to get last N records with activerecord?

JtR picture JtR · Jan 7, 2009 · Viewed 137.1k times · Source

With :limit in query, I will get first N records. What is the easiest way to get last N records?

Answer

Bongs picture Bongs · Jan 27, 2012

This is the Rails 3 way

SomeModel.last(5) # last 5 records in ascending order

SomeModel.last(5).reverse # last 5 records in descending order