Ruby on Rails:如何使用 ActiveRecord 对两列进行排序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3587776/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:06:49  来源:igfitidea点击:

Ruby on Rails: how do I sort with two columns using ActiveRecord?

ruby-on-rails

提问by NullVoxPopuli

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.

我希望能够首先按updated_at 排序,然后,如果同一天出现多个项目,则按价格排序。

采纳答案by Daniel Vandersluis

Assuming you're using MySQL,

假设您使用的是 MySQL,

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

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

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

回答by Christian Fazzini

In Rails 4 you can do something similar to:

在 Rails 4 中,您可以执行以下操作:

Model.order(foo: :asc, bar: :desc)

fooand barare columns in the db.

foo并且bar是数据库中的列。

回答by Erik Escobedo

Thing.find(:all, :order => "updated_at desc, price asc")

will do the trick.

会做的伎俩。

Update:

更新:

Thing.all.order("updated_at DESC, price ASC")

is the Rails 3 way to go. (Thanks @cpursley)

是 Rails 3 的方法。(感谢@cpursley

回答by golfadas

Active Record Query Interfacelets you specify as many attributes as you want to orderyour query:

Active Record Query Interface允许您指定任意数量的属性来对查询进行排序

models = Model.order(:date, :hour, price: :desc)

or if you want to get more specific (thanks @zw963):

或者如果您想获得更具体的信息(感谢@zw963):

models = Model.order(price: :desc, date: :desc, price: :asc) 

Bonus: After the first query, you can chain other queries:

奖励:在第一次查询之后,您可以链接其他查询:

models = models.where('date >= :date', date: Time.current.to_date)

回答by Ruby Racer

Actually there are many ways to do it using Active Record. One that has not been mentioned above would be (in various formats, all valid):

实际上,使用 Active Record 有很多方法可以做到这一点。上面没有提到的一个是(各种格式,都有效):

Model.order(foo: :asc).order(:bar => :desc).order(:etc)

Maybe it's more verbose, but personally I find it easier to manage. SQL gets produced in one step only:

也许它更冗长,但我个人觉得它更容易管理。SQL 只需一步生成:

SELECT "models".* FROM "models" ORDER BY "models"."etc" ASC, "models"."bar" DESC, "models"."foo" ASC

Thusly, for the original question:

因此,对于原始问题:

Model.order(:updated_at).order(:price)

You need not declare data type, ActiveRecord does this smoothly, and so does your DB Engine

您不需要声明数据类型,ActiveRecord 可以顺利完成此操作,您的数据库引擎也是如此

回答by robertokl

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

回答by Max Alexander Hanna

None of these worked for me! After exactly 2 days of looking top and bottom over the internet, I found a solution!!

这些都不适合我!经过整整 2 天在互联网上查看顶部和底部,我找到了一个解决方案!!

lets say you have many columns in the products table including: special_price and msrp. These are the two columns we are trying to sort with.

假设您在产品表中有许多列,包括:special_price 和 msrp。这是我们尝试排序的两列。

Okay, First in your Model add this line:

好的,首先在您的模型中添加以下行:

named_scope :sorted_by_special_price_asc_msrp_asc, { :order => 'special_price asc,msrp asc' }

Second, in the Product Controller, add where you need to perform the search:

其次,在 Product Controller 中,添加您需要执行搜索的位置:

@search = Product.sorted_by_special_price_asc_msrp_asc.search(search_params)