With :limit in query, I will get first N records. What is the easiest way to get last N records?
With :limit in query, I will get first N records. What is the easiest
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Updated Answer (2020)
You can get last N records simply by using last method:
Record.last(N)Example:
User.last(5)Returns 5 users in descending order by their id.
Deprecated (Old Answer)
An active record query like this I think would get you what you want (‘Something’ is the model name):
edit: As noted in the comments, another way: