【发布时间】:2016-09-28 04:33:40
【问题描述】:
我在模型中有以下范围:
class Office < ApplicationRecord
belongs_to :money_pot
has_one :fiscal_year, through: :money_pot
has_one :money_type, through: :money_pot
scope :order_by_fiscal_year_and_money_type, -> {
joins(money_pot: [:fiscal_year, :money_type])
.order("fiscal_years.end_date desc, money_types.short_name")}
end
这个范围确实有效。但是:我想在 order 子句中从“纯字符串”语法切换到“哈希”语法。我认为我遇到了麻烦,因为范围内的order 子句在关联上。
这是我尝试过但没有成功的方法:
scope :order_by_fiscal_year_and_grant_type, -> {
joins(money_pot: [:fiscal_year, :money_type])
.order(fiscal_years: {end_date: :desc}, money_types: {short_name: :asc})}
这是它返回的错误:
方向“{:end_date=>:desc}”无效。有效方向为:[:asc, :desc, :ASC, :DESC, "asc", "desc", "ASC", "DESC"]
我查看了 Active Record Query Interface Rails Guides 的 ordering section 和 hash conditions 部分。
如何将此范围完全转换为哈希语法?
【问题讨论】:
标签: ruby-on-rails ruby activerecord