在post Rails 5.1中,attribute_changed怎么办?更改 "validate" 个回调?

In post Rails 5.1, how does attribute_changed? chage for "validate" callbacks?

在 Rails 4.1 中,我曾经在我的模型中更改属性时调用验证方法

  validate :my_attribute_is_valid, if: :my_attribute_changed?

使用 Rails 5.1 及更高版本(我使用的是 6),attribute_changed?已更改 before_ 和 after_ 回调(分别为 saved_change_to_attribute?和 will_save_change_to_attribute?)。更改“验证”方法检查的正确方法是什么?

正确的是:will_save_change_to_attribute?

save 回调之前验证 运行,因此,您现在还不能检查 saved_changesAvailable callbacks.

并且如 will_save_change_to_attribute? 的文档中所述:

Will this attribute change the next time we save? This method is useful in validations and before callbacks to determine if the next call to save will change a particular attribute. It can be invoked as will_save_change_to_name? instead of will_save_change_to_attribute?("name").