[英]How do I order database records in rails by most recent?
I want to order all the items in a model Item
so it displays the most recent ones first. 我想订购模型Item
中的所有项目,以便它首先显示最新的项目。 According to the Rails guide , the following code should work: 根据Rails指南 ,以下代码应该工作:
Item.order("created_at DESC")
However, when I type that (or varieties) in the terminal, the most recent item always shows up last, and that's how they show up on my page. 但是,当我在终端中键入那些(或多种)时,最新的项目总是最后显示,这就是它们在我的页面上显示的方式。 How do I efficiently retrieve them with he most recent first? 我最近如何有效地检索它们? (I'm going to display only some of them at a time on each page.) (我将在每页上一次只显示其中一些。)
Note that my default scope for items is oldest first. 请注意,我的项目默认范围是最早的。
Update: This is the SQL I get: 更新:这是我获得的SQL:
SELECT "comments".* FROM "comments" ORDER BY comments.created_at ASC, created_at DESC SELECT“comments”。* FROM“comments”ORDER BY comments.created_at ASC,created_at DESC
So I guess I shouldn't use default scopes... 所以我想我不应该使用默认范围......
I modified CDub's answer with reverse
so it now works: 我用reverse
修改了CDub的答案,所以它现在有效:
Item.order(:created_at).reverse
I'm still not sure why the Rails guide's way doesn't work. 我仍然不确定为什么Rails指南的方式不起作用。 Also the above method doesn't work well with pagination. 上述方法也不适用于分页。
The query you posted is correct 您发布的查询是正确的
Item.order("created_at DESC")
The only reason why it would not work is if there is anything else conflicting with it. 它不起作用的唯一原因是它还有其他任何相互矛盾的原因。 In general, the conflict is represented by a default_scope
. 通常,冲突由default_scope
表示。
If you have a default scope that overrides your order, you should first unscope
the query 如果您的默认范围覆盖了您的订单,则应首先unscope
查询
Item.unscoped { Item.order("created_at DESC") }
If you are using default scopes, I strongly encourage you to avoid them. 如果您使用默认范围,我强烈建议您避免使用它们。 They are very hard to debug and unscope. 它们很难调试和解决。
There are very few cases where default scopes make sense. 极少数情况下默认范围是有意义的。 You can simply pass the (default) scope at select time in the controller or create a custom method for it. 您只需在控制器中的选定时间传递(默认)范围,或为其创建自定义方法。
I realise this is a really old question, but none of the answers contain the solution without writing raw SQL, which is available since Rails 3+: 我意识到这是一个非常古老的问题,但没有一个答案包含没有编写原始SQL的解决方案,这可以从Rails 3+开始使用:
Item.order(created_at: :desc)
or using the reverse_order
method: 或使用reverse_order
方法:
Item.order(:created_at).reverse_order
See more at http://guides.rubyonrails.org/active_record_querying.html#ordering and http://guides.rubyonrails.org/active_record_querying.html#reverse-order . 有关详情,请访问http://guides.rubyonrails.org/active_record_querying.html#ordering和http://guides.rubyonrails.org/active_record_querying.html#reverse-order 。
Item.unscoped.order('created_at DESC') should work
。使用反向可能会在记录数增加时降低性能
Correct one and tested 纠正一个并进行测试
@purchase_orders = current_company.purchase_orders.order(:date)
@purchase_orders = @purchase_orders.reverse_order
您可以添加您还可以在项目模型中定义默认顺序
default_scope order('created_at DESC')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.