如何获得最后的N条记录与ActiveRecord的? [英] How to get last N records with activerecord?

查看:117
本文介绍了如何获得最后的N条记录与ActiveRecord的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用:限在查询中,我会第一个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

这篇关于如何获得最后的N条记录与ActiveRecord的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆