我想执行 update_all 查询以更新日期时间列,在所有行的该列的当前值上增加 1 天.
I want to execute an update_all query to update a datetime column adding 1 day to the current value of this column on all rows.
我可以使用 .each 和 .update 实现这一点,但在单个查询中执行会更有效.
I could achieve that using .each and .update, but it would be more efficient to perform in a single query.
是否可以使用 Rails 和 PostgreSQL 实现这一点?
Is it possible to achieve that using Rails and PostgreSQL?
推荐答案Rails 更新所有记录的方式
Rails way of updating all records
Model.update_all("column_name = (column_name + '1 DAY'::INTERVAL)")此更新查询可以通过 rails 控制台运行.
This update query can be run via rails console.
注意:update_all 方法不会触发 Active Record 回调或验证,也不会更改 updated_at 时间戳.
Note: update_all method does not trigger Active Record callbacks or validations and it will not change updated_at timestamp.
更多推荐
Rails/PostgreSQL
发布评论