Ruby-on-rails 如何使用activerecord获取最后N条记录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/420352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get last N records with activerecord?
提问by JtR
With :limitin query, I will get first N records. What is the easiest way to get last N records?
随着:limit查询,我会得到前N个记录。获取最后 N 条记录的最简单方法是什么?
采纳答案by Dan McNevin
An active record query like this I think would get you what you want ('Something' is the model name):
我认为这样的活动记录查询可以满足您的需求('Something' 是模型名称):
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
回答by Bongs
This is the Rails 3 way
这是 Rails 3 的方式
SomeModel.last(5) # last 5 records in ascending order
SomeModel.last(5).reverse # last 5 records in descending order
回答by Arthur Neves
new way to do it in rails 3.1 is SomeModel.limit(5).order('id desc')
在 rails 3.1 中做到这一点的新方法是 SomeModel.limit(5).order('id desc')
回答by Gagan Gami
For Rails 4 and above version:
对于Rails 4 及以上版本:
You can try something like this If you want first oldest entry
你可以尝试这样的事情如果你想要第一个最旧的条目
YourModel.order(id: :asc).limit(5).each do |d|
You can try something like this if you want last latest entries..
如果你想要最新的条目,你可以尝试这样的事情..
YourModel.order(id: :desc).limit(5).each do |d|
回答by Amin Ariana
For Rails 5 (and likely Rails 4)
对于 Rails 5(可能还有 Rails 4)
Bad:
坏的:
Something.last(5)
because:
因为:
Something.last(5).class
=> Array
so:
所以:
Something.last(50000).count
will likely blow up your memory or take forever.
可能会破坏你的记忆或永远。
Good approach:
好办法:
Something.limit(5).order('id desc')
because:
因为:
Something.limit(5).order('id desc').class
=> Image::ActiveRecord_Relation
Something.limit(5).order('id desc').to_sql
=> "SELECT \"somethings\".* FROM \"somethings\" ORDER BY id desc LIMIT 5"
The latter is an unevaluated scope. You can chain it, or convert it to an array via .to_a. So:
后者是一个未评估的范围。您可以链接它,或通过 将其转换为数组.to_a。所以:
Something.limit(50000).order('id desc').count
... takes a second.
...需要一秒钟。
回答by Developer
Solution is here:
解决方案在这里:
SomeModel.last(5).reverse
Since rails is lazy, it will eventually hit the database with SQL like: "SELECT table.* FROM tableORDER BY table.idDESC LIMIT 5".
由于 rails 是惰性的,它最终会使用 SQL 命中数据库,例如:“SELECT table.* FROM tableORDER BY table. idDESC LIMIT 5”。
回答by Thorin
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
回答by timlentse
In my rails (rails 4.2)project, I use
在我的 rails(rails 4.2)项目中,我使用
Model.last(10) # get the last 10 record order by id
and it works.
它有效。
回答by Joydeep Banerjee
we can use Model.last(5)or Model.limit(5).order(id: :desc)in rails 5.2
我们可以在 Rails 5.2 中使用Model.last(5)或Model.limit(5).order(id: :desc)
回答by Thorin
Just try:
你试一试:
Model.order("field_for_sort desc").limit(5)

