How to get last N records with activerecord?

前端 未结 14 674
南旧
南旧 2020-12-12 11:43

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

14条回答
  •  悲&欢浪女
    2020-12-12 11:57

    If you need to set some ordering on results then use:

    Model.order('name desc').limit(n) # n= number
    

    if you do not need any ordering, and just need records saved in the table then use:

    Model.last(n) # n= any number
    

提交回复
热议问题