Touch records without running callbacks

Updated . Posted . Visible to the public.

ActiveRecord comes with a method touch Show archive.org snapshot which sets the updated_at timestamp to the current time. Unfortunately it also runs callbacks (and hence validations) on the receiving record, so it is unsuitable if you call it very often.

Use the attached initializer to get a touch_gently method which updates updated_at, but does not run callbacks:

User.find(5).touch_gently

The initializer will also give you a method touch_all_gently which touches a scope of records in a single query (also without running callbacks):

User.active.touch_all_gently
Henning Koch
Last edit
Attachments
License
Source code in this card is licensed under the MIT License.
Posted by Henning Koch to makandra dev (2011-03-24 09:43)