Today I Learned

A Hashrocket project

Rails offset method

Rails has a method for skipping records in a query, ‘offset’. It is available through the ActiveRecord::QueryMethods library.

User.offset(15) # generated SQL has "OFFSET 15"

The docs recommend using it with ‘order’. I used the two methods today to return all blog posts, excluding the ten most recent:

Post.order('created_at desc').offset(10)
Looking for help? Hashrocket has been an industry leader in Ruby on Rails since 2008. Rails is a core skill for each developer at Hashrocket, and we'd love to take a look at your project. Contact us and find out how we can help you.