Ruby on Rails ActiveRecord Query Interface Limit and Offset


Example

You can use limit to tell the number of records to be fetched, and use offset to tell the number of records to skip before starting to return the records.

For Example

User.limit(3) #returns first three records

It will generate following sql query.

"SELECT  `users`.* FROM `users` LIMIT 3"

As offset is not mentioned in above query so it will return first three records.

User.limit(5).offset(30) #returns 5 records starting from 31th i.e from 31 to 35

It will generate following sql query.

"SELECT  `users`.* FROM `users` LIMIT 5 OFFSET 30"