通过ActiveRecord的返回日期分类数组(或任何其它列)数组、日期、ActiveRecord

2023-09-09 22:02:22 作者:人不嘚瑟枉少年

我如何排序由 created_at 日期列由ActiveRecord的查询返回数组?

How can I sort an array returned by an ActiveRecord query by a created_at date column?

这一次发生在查询被执行。

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.

推荐答案

红宝石包括整理出框的支持。

Ruby includes support for sorting out of the box.

sorted = @records.sort_by &:created_at

然而,这不会出现有很大程度上与显示器和可能属于在控制器中。

However, this doesn't appear to have much to do with display and probably belongs in the controller.

 
精彩推荐
图片推荐