Rails 5 - 如何在 before_save 回调条件上否定方法调用

rmc*_*rry 1 ruby-on-rails callback before-save

在我的模板模型中,我有这个回调:

  before_save :set_status, if: :is_template?

  private

  def is_template?
    return self.template_type == 'template'
  end
Run Code Online (Sandbox Code Playgroud)

如何更改它,使其仅在 template_type 不是“模板”时触发?

我尝试过这些:

1 before_save :set_status, if: !:is_template?
2 before_save :set_status, if: !(:is_template?)
Run Code Online (Sandbox Code Playgroud)

但它们都会导致“before_save找不到方法”错误。

读完这个问题后,我也尝试过这个:

  before_save :set_status, if: Proc.new {|model| !model.is_template? }
Run Code Online (Sandbox Code Playgroud)

但对于这样一个简单的案例来说,这似乎有些过分了。

我真的需要编写另一种方法:is_not_template?才能使其工作吗?

Div*_*ero 5

尝试before_save :set_status, unless: :is_template?;)