简体   繁体   English

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

[英]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仅从您的查询中获取约束 ,而忽略grouphaving子句。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM