Ruby on rails 4 带_选项的轨道条件问题

Ruby on rails 4 带_选项的轨道条件问题,ruby-on-rails-4,callback,Ruby On Rails 4,Callback,我有两个回调调用相同的方法,我想用相同的条件控制它们。下面是示例代码 with_options if: :survey_enabled? do after_save: #some_method, unless: Proc.new { |role| role.role_name == "Manager" } after_save: #some_method, unless: Proc.new { |role| role.role_name == "Manager" } end priva

我有两个回调调用相同的方法,我想用相同的条件控制它们。下面是示例代码

with_options if: :survey_enabled? do
  after_save: #some_method, unless: Proc.new { |role| role.role_name == "Manager" }
  after_save: #some_method, unless: Proc.new { |role| role.role_name == "Manager" }
end

private

def survey_enabled?
 Rails.configuration.survey_enabled
end

def some_method
 #my code
end
你是残疾人吗?在为false:FalseClass保存后返回false am get
未定义的方法

我试着在一个块中调用这个方法(因为没有这个块,一旦类被初始化,如果我没有错的话,这些方法就会被调用),比如


但我还是遇到了同样的问题。我可以知道这里有什么问题吗?

使用
with_options
是,如果要调用同一对象上的一系列操作,可以使用
with_options
调用它

例如:(来自)

因此,这里缺少必须执行操作的对象,在上述情况下,该对象是user

with_options if: :survey_enabled? do
  after_save: #some_method, unless: Proc.new { |role| role.role_name == "Manager" }
  after_save: #some_method, unless: Proc.new { |role| role.role_name == "Manager" }
end
同样,此消息在为false:false类保存后会显示
未定义的方法。块试图在对false执行保存操作后调用,false是FalseClass的对象


希望对您有所帮助使用
with_options
是,如果您想调用同一对象上的一系列操作,可以使用
with_options
调用它

例如:(来自)

因此,这里缺少必须执行操作的对象,在上述情况下,该对象是user

with_options if: :survey_enabled? do
  after_save: #some_method, unless: Proc.new { |role| role.role_name == "Manager" }
  after_save: #some_method, unless: Proc.new { |role| role.role_name == "Manager" }
end
同样,此消息在为false:false类保存后会显示
未定义的方法。块试图在对false执行保存操作后调用,false是FalseClass的对象


希望它会有帮助

那么你的意思是传递对象可以解决问题?如果是这样的话,那么我也尝试过这样做,那么你的意思是传递对象将解决问题?如果是这样的话,那么我也尝试过这样做
with_options if: :survey_enabled? do
  after_save: #some_method, unless: Proc.new { |role| role.role_name == "Manager" }
  after_save: #some_method, unless: Proc.new { |role| role.role_name == "Manager" }
end