我想按两列排序,一个是小数(updated_at
),另一个是小数( DateTime )
我希望能够首先按updated_at排序,然后,如果同一天出现多个项目,按价格排序。
发布于 2013-10-18 16:45:21
在Rails 4中,您可以执行类似的操作:
Model.order(foo: :asc, bar: :desc)
foo
和bar
是数据库中的列。
发布于 2010-08-28 04:17:15
Thing.find(:all, :order => "updated_at desc, price asc")
就能达到目的。
更新:
Thing.all.order("updated_at DESC, price ASC")
是Rails的3条路要走。(感谢@cpursley)
发布于 2013-07-12 10:36:42
Active Record Query Interface允许您指定任意数量的属性,以便对查询执行order操作:
models = Model.order(:date, :hour, price: :desc)
或者,如果您想要更具体(感谢@zw963 ):
models = Model.order(price: :desc, date: :desc, price: :asc)
额外好处:在第一个查询之后,您可以链接其他查询:
models = models.where('date >= :date', date: Time.current.to_date)
https://stackoverflow.com/questions/3587776
复制相似问题