我想做一个
update ... from
,或使用CTE但使用ActiveRecord的更新。理想情况下,这样我就可以利用
in_batches
.
例如,Person有很多东西,我想缓存它们的第一个Thing。我想它会像这样工作。
Person
.in_batches
.from(
Thing.select(
:id,
:people_id,
"rank() over (partition by people_id order by created_at asc) as thing_rank"
)
)
.where(
"things.thing_rank": 1,
"people.id": :people_id
)
.update_all(cached_thing_id: "things.id")
但我想不出任何组合是有效的。
或者,有没有一种方法可以使用
in_batches
同时手动写出SQL?而且我没有附加到子查询方法,CTE或临时视图也可以。