如何获得最后的N条记录与ActiveRecord的?如何获得、ActiveRecord

2023-09-08 18:52:12 作者:听弦断,断那三千痴缠

使用:限在查询中,我会第一个N个记录。什么是拿到最后的N条记录的最简单的方法?

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

推荐答案

像这样的活动记录查询我想你想要什么会让你('东西'是型号名称):

An active record query like this I think would get you what you want ('Something' is the model name):

Something.find(:all, :order => "id desc", :limit => 5).reverse

修改:正如评论指出,另一种方式:

edit: As noted in the comments, another way:

result = Something.find(:all, :order => "id desc", :limit => 5)

while !result.empty?
        puts result.pop
end