Rails: Is there an equivalent to save_without_validation that skips after_save filters?

I have an after_save filter that I do not want to run in a specific instance. Is there a way to do this similarly to save_without_validation?

Thanks,

+4
source share
4 answers

When using rails 2, you can call the private create_without_callbacks method by doing:

 @my_obj.send(:create_without_callbacks) 
+1
source

There is a good example of an ActiveRecord extension to provide skipping callbacks here: http://weareintegrum.com/?p=10

The idea is to create an ActiveRecord method called skip_callback that takes a block:

 def self.skip_callback(callback, &block) method = instance_method(callback) remove_method(callback) if respond_to?(callback) define_method(callback){ true } begin result = yield ensure remove_method(callback) define_method(callback, method) end result end 

Then everything you do in the block does not call back.

0
source

You can set and reset callbacks as follows:

  Post.after_update.reject! {|callback| callback.method.to_s == 'fancy_callback_on_update' } Post.after_create.reject! {|callback| callback.method.to_s == 'fancy_callback_on_create' } Post.after_create :fancy_callback_on_create Post.after_update :fancy_callback_on_update 

You can add them around your custom save method.

0
source

For Rails 2, you can also use the following methods:

create_without_callbacks or update_without_callbacks

0
source

All Articles