[英]How to pass a variable parameter value using the update_all() method in Rails?
[英]rails update_all for ActiveRecord Relation using 'having' don't work
样品:
a = Model.join("...").where("...").group("...")
b = Model.join("...").where("...").group("...").having("...")
如果我做:
a.class
给了我ActiveRecord :: Relation。 与b.class
相同。
当我做:
a.length
我得到1000 。 和b.length
我得到50
最后,如果我这样做:
a.update_all(field:'...')
=> 1000
b.update_all(field:'...')
=> 1000
并非我所期望的50 。
为什么会这样? 有什么办法可以解决这个问题?
update_all
仅从您的查询中获取约束 ,而忽略group
和having
子句。
这是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
我猜您将必须分两步来执行此操作,使用having
子句执行查询并获取匹配的50条记录的列表ID,然后使用IN
子句对这些记录执行update_all
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.