0

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)
Fellow Stranger
  • 32,129
  • 35
  • 168
  • 232
  • 2
    http://blog.bigbinary.com/2009/01/21/override-automatic-timestamp-in-activerecord-rails.html – usha Nov 26 '14 at 16:55
  • Possible duplicate of [Is there a way to avoid automatically updating Rails timestamp fields?](http://stackoverflow.com/questions/861448/is-there-a-way-to-avoid-automatically-updating-rails-timestamp-fields) – Dave Schweisguth May 12 '16 at 18:47

2 Answers2

1

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

Community
  • 1
  • 1
aspencer8111
  • 786
  • 4
  • 9
1

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

Stanislav Mekhonoshin
  • 4,276
  • 2
  • 20
  • 25