按日期(或任何其他列)对 ActiveRecord 返回的数组进行排序 [英] Sort array returned by ActiveRecord by date (or any other column)

查看:14
本文介绍了按日期(或任何其他列)对 ActiveRecord 返回的数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何按 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 支持开箱即用的排序.

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.

这篇关于按日期(或任何其他列)对 ActiveRecord 返回的数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆