Skip to content
Advertisement

Rails/PostgreSQL – Query to update all dates adding 1 day

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.

I could achieve that using .each and .update, but it would be more efficient to perform in a single query.

Is it possible to achieve that using Rails and PostgreSQL?

Advertisement

Answer

Rails way of updating all records

Model.update_all("column_name = (column_name + '1 DAY'::INTERVAL)")

This update query can be run via rails console.

Note: update_all method does not trigger Active Record callbacks or validations and it will not change updated_at timestamp.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement