Rails/PostgreSQL - 查询更新所有添加 1 天的日期 [英] Rails/PostgreSQL - Query to update all dates adding 1 day

查看:82
本文介绍了Rails/PostgreSQL - 查询更新所有添加 1 天的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想执行 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 - 查询更新所有添加 1 天的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆