Rails 4: Skip callback
skip_callback
is a complicated and not granular option.
I prefer to use an attr_accessor:
attr_accessor :skip_my_method, :skip_my_method_2
after_save{ my_method unless skip_my_method }
after_save{ my_method_2 unless skip_my_method_2 }
That way you can be declarative when skipping a callback:
model.create skip_my_method: true # skips my_method
model.create skip_my_method_2: true # skips my_method_2
How I can skip callback in action
This should work:
class BarsController < ApplicationController
after_action :some_method, only: [:index], unless: :skip_action?
def index
get_cache = $redis.get('some_key')
if get_cache.present?
@skip_action = true
# i want to skip after_action callback in here
else
# other stuff
end
end
private
def skip_action?
@skip_action
end
end
You can also use attr_accessor :skip_action
instead of private method because controller is just object.
How to skip ActiveRecord callbacks?
For Rails 2, but not Rails 3 you can use these:
object.send(:create_without_callbacks)
object.send(:update_without_callbacks)
Skip callback on create Rails
Based on the referenced answer:
FactoryGirl.define do
factory :withdrawal_request, class: 'WithdrawalRequest' do
...
after(:build) { WithdrawalRequest.skip_callback(:create, :before, :callback_to_be_skipped) }
#you were getting the errors here initially because you were calling the method on Class, the superclass of WithdrawalRequest
#OR
after(:build) {|withdrawal_request| withdrawal_request.class.skip_callback(:create, :before, :callback_to_be_skipped)}
end
end
Is it possible to skip role removing using callbacks with rolify gem for Rails
It looks like you are hoping to use these callbacks like an ActiveRecord validation. They are different things. The callback is simply a way to run code before and after the role record is created/destroyed, it is not used to control whether the mutation code within is run.
Some options:
- Add logic to your model to test if it
can_remove_role?(:role)
and then test that in your controller before making the change. - If you are saving from a form, you can make a validation on the model to check your condition. ie. The User model would be invalid if that was what changed. Check out the methods on Dirty for inspiration on how to see what did/will change https://api.rubyonrails.org/classes/ActiveModel/Dirty.html
- Use a policy in something like the Pundit gem or similar authorization package (which your program should have if it is to be used in a real environment) to wrap changing of roles. ie. in the Pundit example your UserPolicy would have a
change_role?
or similar policy where you define what is a legal role change for the@record
and then call the authorize on it.
Option 3 is nice, because it provides a DRY location to make more rules regarding what can and cannot be done with your Users. Perhaps do some searches for tutorials on how Pundit and Rolify can be used together.
Related Topics
How to 'Join' an Array Adding to the Beginning of the Resulting String the First Character to Join
Rvm Isnt Setting Environment with Cron
Ruby: Length of a Line of a File in Bytes
Rubymine 6.0.2, Unable to Debug
I Trying to Make a Code That Gives the User a Personal Number After They Have Made an User
Sort by Date in Descending Order in Ruby in Rails
How Does Sinatra Define and Invoke the Get Method
Bundler::Rubyversionmismatch: Your Ruby Version Is 1.9.3, But Your Gemfile Specified 2.0.0
Rails Cannot Find Model with Same Name as Ruby Class
How to Find the Most Recently Modified Folder in a Directory Using Ruby
How to Split a String of Repeated Characters with Uneven Amounts? Ruby
How to Update a Model's Attribute with a Virtual Attribute
Use [].Replace to Make a Copy of an Array
How to Print All the Staged File Names Using Ruby Git Pre-Commit Hook
How to Cancel Evaluating a Required Ruby File? (A.K.A. Top-Level Return)
Ruby 2.4 on MAC Os Mojave:Image Not Found ... Related to Openssl.Bundle