Ruby on rails “包含表-方向”的顺序和限制;{:processed_on=>;:desc}";无效

Ruby on rails “包含表-方向”的顺序和限制;{:processed_on=>;:desc}";无效,ruby-on-rails,ruby-on-rails-4,activerecord,Ruby On Rails,Ruby On Rails 4,Activerecord,我有以下几点 用户有很多衡量标准 我想找到1行的所有用户,这些指标具有order('processed_on desc')和限制(1) 我在尝试什么 User.includes(:metrics).where(status: 'active').order(:metrics => { processed_on: :desc }).limit(1) 错误: 方向“{:processed_on=>:desc}”无效。有效的指示是:[:asc,:desc,:asc,:desc,“asc”,“d

我有以下几点

用户有很多衡量标准

我想找到
1行
的所有用户,这些
指标
具有
order('processed_on desc')
限制(1)

我在尝试什么

User.includes(:metrics).where(status: 'active').order(:metrics => { processed_on: :desc }).limit(1)
错误:


方向“{:processed_on=>:desc}”无效。有效的指示是:[:asc,:desc,:asc,:desc,“asc”,“desc”,“asc”,“desc”]

您不能将哈希语法与
顺序一起使用,而是直接传递SQL:

User.includes(:metrics).where(status: 'active').order('metrics.processed_on DESC').limit(1)
您可能仍然会从数据库中得到一个错误,该错误与from子句中缺少的
关系度量有关。如果是这样,请将
引用(:metrics)
语句也添加到查询中:

User.includes(:metrics).references(:metrics).where(status: 'active').order('metrics.processed_on DESC').limit(1)

另外,调用
.limit(1)
将返回长度为1的数组。您可能需要考虑只使用<代码>。第一个< /C> >。

< P>不能使用哈希语法和<代码>订单< /> >,而不是直接传递SQL:

User.includes(:metrics).where(status: 'active').order('metrics.processed_on DESC').limit(1)
您可能仍然会从数据库中得到一个错误,该错误与from子句中缺少的
关系度量有关。如果是这样,请将
引用(:metrics)
语句也添加到查询中:

User.includes(:metrics).references(:metrics).where(status: 'active').order('metrics.processed_on DESC').limit(1)

另外,调用
.limit(1)
将返回长度为1的数组。你可能想考虑只使用<代码>。第一个< /COD>取而代之。

我认为你可以在ActhVIECORD 4的OrthEnter子句中使用哈希语法,你可以使用哈希语法,但是,如果散列与
where
joins
子句中的查询不同,那么它将不起作用。我认为,您可以在ActiveRecord 4中的order子句中使用散列语法,但如果散列与
where
joins
子句中的查询不同,那么它将不起作用。