[英]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.