How to get last N records with activerecord?

前端 未结 14 660
南旧
南旧 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 12:24

    This is the Rails 3 way

    SomeModel.last(5) # last 5 records in ascending order
    
    SomeModel.last(5).reverse # last 5 records in descending order
    

提交回复
热议问题