[英]rails update_all for ActiveRecord Relation using 'having' don't work
Sample: 样品:
a = Model.join("...").where("...").group("...")
b = Model.join("...").where("...").group("...").having("...")
If I do: 如果我做:
a.class
gives me ActiveRecord::Relation. a.class
给了我ActiveRecord :: Relation。 Same with b.class
. 与
b.class
相同。
When I do: 当我做:
a.length
i get 1000 . a.length
我得到1000 。 And b.length
i get 50 和
b.length
我得到50
And finally, if i do: 最后,如果我这样做:
a.update_all(field:'...')
=> 1000
b.update_all(field:'...')
=> 1000
Not 50 , as I was expecting. 并非我所期望的50 。
Why this happen? 为什么会这样? Is there any way to deal with this?
有什么办法可以解决这个问题?
update_all
only takes the constraints from your query and ignores the group
and having
clauses. update_all
仅从您的查询中获取约束 ,而忽略group
和having
子句。
Here is the source code of update_all
这是
update_all
的源代码
def update_all(updates)
.....
# HERE IS THE RELEVANT CODE
# It extracts only the constraints, limit and order clauses. It ignores the rest
stmt.take(arel.limit)
stmt.order(*arel.orders)
stmt.wheres = arel.constraints
.....
end
I guess you would have to do it in two steps, execute your query with the having
clause and get the list IDs for the 50 records that match and then do an update_all
on these records using an IN
clause 我猜您将必须分两步来执行此操作,使用
having
子句执行查询并获取匹配的50条记录的列表ID,然后使用IN
子句对这些记录执行update_all
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.