Fellow Stranger
Fellow Stranger

Reputation: 34013

Update object, while retaining the update_at timestamp

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)

Upvotes: 0

Views: 109

Answers (2)

Stanislav Mekhonoshin
Stanislav Mekhonoshin

Reputation: 4386

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

Upvotes: 1

aspencer8111
aspencer8111

Reputation: 786

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

Upvotes: 1

Related Questions