Update object, while retaining the update_at timestamp

121 views Asked by At

I want to update an attribute on an object, but keep the attribute updated_at (DateTime type) unchanged.

This doesn't seem to work, the updated_at gets current date/time:

p = Post.first
p.update(updated_at: p.updated_at, published: true)
2

There are 2 answers

0
aspencer8111 On BEST ANSWER

Asked and answered here: Is there a way to avoid automatically updating Rails timestamp fields?

But the short answer is to use update_columns . i.e. user.update_columns(last_request_at: Time.current)

More info here: http://api.rubyonrails.org/classes/ActiveRecord/Persistence.html#method-i-update_column

2
Stanislav Mekhonoshin On

Just use update_all method, it does not touch updated_at field http://apidock.com/rails/ActiveRecord/Base/update_all/class