I have some models that have after_save callbacks. Usually that's fine, but in some situations, like when creating development data, I want to save the models without having the callbacks run. Is there a simple way to do that? Something akin to…
Person#save( :run_callbacks => false )
or
Person#save_without_callbacks
I looked in the Rails docs and didn't find anything. However in my experience the Rails docs don't always tell the whole story.
UPDATE
I found a blog post that explains how you can remove callbacks from a model like this:
Foo.after_save.clear
I couldn't find where that method is documented but it seems to work.
Best Answer
Use
update_column
(Rails >= v3.1) orupdate_columns
(Rails >= 4.0) to skip callbacks and validations. Also with these methods,updated_at
is not updated.http://api.rubyonrails.org/classes/ActiveRecord/Persistence.html#method-i-update_column
#2: Skipping callbacks that also works while creating an object
UPDATE (2020)
Apparently Rails has always supported
:if
and:unless
options, so above code can be simplified as: