Ruby on Rails - limit and offset in ruby on rails- ruby on rails tutorial - rails guides - rails tutorial - ruby rails
Limit and Offset
- We 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.
Example
User.limit(3) #returns first three recordsUser.limit(3) #returns first three records
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
It will generate following sql query
"SELECT `users`.* FROM `users` LIMIT 3"
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
- 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
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
It will generate following sql query.
"SELECT `users`.* FROM `users` LIMIT 5 OFFSET 30"