“[属性]_改变了吗?”使用 CarrierWave mount_uploader :[attribute]?

How does "[attribute]_changed?" work with CarrierWave mount_uploader :[attribute]?

我的模型中有 CarrierWave 上传器。

before_validation :parse_template_file, if: :template_file_changed?
mount_uploader :template_file

我上面的代码无法抛出 NoMethodError template_file_changed?

如何在保存记录前检查文件是否被更改?

some_attr_changed? 是一个实例方法,因此您需要在模型的实例上调用它。

所以,而不是这个...

before_validation :parse_template_file, if: :template_file_changed?

尝试这样的事情...

before_validation :parse_template_file, if: :file_changed?

private

def file_changed?
  self.template_file_changed?  # self can be implied
end