Ruby-on-rails – How to get last N records with activerecord

activerecordrubyruby-on-rails

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

Best Answer

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