Ruby on Rails:如何使用 ActiveRecord 对两列进行排序? [英] Ruby on Rails: how do I sort with two columns using ActiveRecord?

查看:25
本文介绍了Ruby on Rails:如何使用 ActiveRecord 对两列进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想按两列排序,一列是日期时间(updated_at),另一列是十进制(价格)

I want to sort by two columns, one is a DateTime (updated_at), and the other is a Decimal (Price)

我希望能够先按 updated_at 排序,然后,如果同一天出现多件商品,则按价格排序.

I would like to be able to sort first by updated_at, then, if multiple items occur on the same day, sort by Price.

推荐答案

假设您使用的是 MySQL,

Assuming you're using MySQL,

Model.all(:order => 'DATE(updated_at), price')

注意与其他答案的区别.updated_at 列将是一个完整的时间戳,因此如果您想根据更新的 进行排序,您需要使用一个函数来获取日期部分时间戳.在 MySQL 中,即 DATE().

Note the distinction from the other answers. The updated_at column will be a full timestamp, so if you want to sort based on the day it was updated, you need to use a function to get just the date part from the timestamp. In MySQL, that is DATE().

这篇关于Ruby on Rails:如何使用 ActiveRecord 对两列进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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