簡體   English   中英

使用“具有”的ActiveRecord關系的rails update_all不起作用

[英]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僅從您的查詢中獲取約束 ,而忽略grouphaving子句。

這是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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM