Veerasundaravel's Ruby on Rails Weblog

July 2, 2014

Rails – Force update ActiveRecord updated_at column

Filed under: Ruby On Rails — Tags: , , , , , , — Veerasundaravel @ 12:11 am

When we usually modify any specific column/field in our model, Active-record will check whether the corresponding value is new or not.

If it is new then only it will create a DB query and update that value with current timestamp in updated_at column, if there is no change in existing value means it will only fire commit query only. There wont be any change in updated_at column.

So if we want modify the timestamp of updated_at field without updating any field means, we can follow any of below two methods:

Method 1:
user.update_attributes(:name => “same old name”, :updated_at => Time.now)

Method 2:
user.touch

Both of above two methods will modify updated_at value with current timestamps even if there is any change in other fields or not.

 

1 Comment »

  1. Thanks!

    Comment by David — July 25, 2017 @ 6:06 am


RSS feed for comments on this post. TrackBack URI

Leave a reply to David Cancel reply