繁体   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