简体   繁体   English

如何最近在rails中订购数据库记录?

[英]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#orderinghttp://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.

相关问题 如何在rails中重新排序表格数据,以便最新数据显示在顶部? - How do I re-order tabular data within rails so that the most recent data appears at the top? 如何按最近日期排序表列? - How do I order table columns by the most recent date? 在Rails中,如何检索模型中的10个最新条目? - In Rails, how do I retrieve 10 most recent entries in a model? Rails / ActiveRecord:如何按与相关模型的最多关联来排序记录? - Rails/ActiveRecord: How do I order records by the most associations with a related model? Rails选择最近的不同和顺序 - Rails select distinct and order by most recent 如何获取关联组中的最新记录 - How to get most recent records in a group on an association 如何在 Rails 中选择通过连接到具有多个记录的另一个表的最新记录 - How to select the most recent record in Rails that's via a join to another table with multiple records 如何从Rails中的Ruby中获取数据库的最新记录以及在何处编写此查询 - how to fetch most recent records from db in ruby on rails and where to write this query 在Rails上获取不同用户的最新记录 - Pull most recent records for distinct users in ruby on rails Ruby-on-Rails:如何从数据库表的有限子集中提取最新条目 - Ruby-on-Rails: How to pull out most recent entries from a limited subset of a database table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM