Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force a record to save itself even if I have not changed any attributes

In order to clean up some bad data I added a before save callback. Now I need to force all the models to be saved again. However no update operation happens if I do this

User.first.save

How do I force all the models to perform save operation even though I don't have any attributes changed.

like image 926
Roger Avatar asked Oct 31 '09 16:10

Roger


2 Answers

You should be able to use touch, it fires callbacks when it saves.

Alternatively turn off partial_updates: ActiveRecord::Base.partial_updates = false

like image 76
cwninja Avatar answered Nov 04 '22 05:11

cwninja


As Josh Rickard commented to the accepted answer, since Rails 3.0.X touch does not invoke validation and callbacks anymore.

You can, however, "touch" it manually with model.update_attribute(:updated_at,Time.now) and skip validation and invoke callbacks that way.

like image 19
parapet Avatar answered Nov 04 '22 04:11

parapet