3

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?

1
  • 1
    This is trivial to express as a query. It should be pretty basic in Ruby as well. Commented Feb 27, 2018 at 15:11

2 Answers 2

8
UPDATE table SET date_field = date_field + 1;

Or ..

UPDATE table SET date_field = date_field + INTERVAL '1 DAY';
Sign up to request clarification or add additional context in comments.

2 Comments

just wondering, as I wasn't aware of such use; at first the +1 (without any specification) looks quite random
I also found it out by accidentally running SELECT CURRENT_DATE + 1 ; :-)
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.