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.