Ruby-on-rails 按日期(或任何其他列)对 ActiveRecord 返回的数组进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1278510/
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
Sort array returned by ActiveRecord by date (or any other column)
提问by user94154
How can I sort an array returned by an ActiveRecord query by a created_atdate column?
如何按created_at日期列对 ActiveRecord 查询返回的数组进行排序?
This occurs once the query has been executed.
一旦执行了查询,就会发生这种情况。
Please don't tell me to do it in the query because I need this to happen in the view.
请不要告诉我在查询中执行此操作,因为我需要在视图中执行此操作。
回答by Chuck
Ruby includes support for sorting out of the box.
Ruby 支持开箱即用的排序。
sorted = @records.sort_by &:created_at
However, this doesn't appear to have much to do with display and probably belongs in the controller.
但是,这似乎与显示没有太大关系,可能属于控制器。
回答by shulmang
While Ruby Enumerableis awesome, ActiveRecord queries will actually return an ActiveRecord::Relation whose query will not have been evaluated yet (Lazy Loading) and can have the order method called on it to off-load this processing to the database where it will scale much better than an Enumerablebased strategy.
虽然 Ruby Enumerable很棒,但 ActiveRecord 查询实际上会返回一个 ActiveRecord::Relation,它的查询还没有被评估(延迟加载),并且可以调用 order 方法来将这个处理卸载到将扩展的数据库中比基于Enumerable的策略要好得多。
Using Enumerable for sorting also confounds doing pagination in the database. There is nothing to prevent the order strategy from being applied in the view. However, I would tend to put this in scope on the model.
使用 Enumerable 进行排序也会混淆在数据库中进行分页。没有什么可以阻止在视图中应用订单策略。但是,我倾向于将其放在模型的范围内。
sorted = @records.order(:created_at)
回答by Bill D
Just call sort on the collection, passing in the block of code which tells Ruby how you want it to sort:
只需对集合调用 sort ,传入告诉 Ruby 您希望它如何排序的代码块:
collection.sort { |a,b| a.created_at <=> b.created_at }
回答by Sajjad Murtaza
Please look at this one and also check complexities.
请看这个并检查复杂性。
Model.all.sort_by{|m| m.created_at} #=> O(log n)
#versus
Model.order(“created_at DESC”) #=> O(1)
回答by Dmitriy Gusev
The best way sorting ActiveRecord array is using default method order
排序 ActiveRecord 数组的最佳方法是使用默认方法顺序
@users.order(:created_at)
@users.order(:created_at)
It's the quickest and the most right solution, because in that case it returns sorted array from db and you no need to use any other operation for that in the Class, for example if you will use suggested sort_byit will loop throw each element of array, and after that it won't be an ActiveRecord array, not cool in my opinion.
这是最快和最正确的解决方案,因为在这种情况下,它从 db 返回排序的数组,并且您无需在类中使用任何其他操作,例如,如果您将使用建议sort_by,它将循环抛出数组的每个元素,之后它不会是一个 ActiveRecord 数组,在我看来并不酷。
ordercan use strings and sumbols, it's very useful, and it takes multiple parameters
order可以使用字符串和sumbols,非常有用,并且需要多个参数
@users.order('created_at asc, first_name desc, last_name asc')
@users.order('created_at asc, first_name desc, last_name asc')

