Ruby on Rails 3.x Skip callback and validation and reset Callback | Ruby on Rails, Skip callback, validation, reset Callback
This will work for Rails 3.x onwords
Model.skip_callback(“create”,:after,:trigger_my_method)
in the above line ‘create’ and ‘after’ represents callback name so it represents ‘after_create’ callback, and the callback method is ‘trigger_my_method’, so totally the above line skips below callback
after_create :trigger_my_method
You can skip validations just by passing :validate => false as parameter to save method
Model.save(:validate => false)
As before skipping callback, you can reset same callback for reset by below ling
Model.set_callback(“create”,:after,:trigger_my_method)
This line will reset the below callback
after_create :trigger_my_method
Example:-
Skip callback and validation and rest the skiped callback
Post.skip_callback(“create”,:after,:trigger_my_method)
post=Post.new(params[:post])
post.save(:validate => false)
Post.set_callback(“create”,:after,:trigger_my_method)
Comments
Post a Comment