简体   繁体   English

回调后的`attribute_changed?`的行为将会改变

[英]The behavior of `attribute_changed?` inside of after callbacks will be changing

I just upgraded a project to Rails 5.1.0 and I'm seeing this deprecation warning. 我刚刚将一个项目升级到Rails 5.1.0,我看到了这个弃用警告。

DEPRECATION WARNING: The behavior of `attribute_changed?` 
inside of after callbacks will be changing in the next version of Rails.
The new return value will reflect the behavior of calling the method 
after `save` returned (e.g. the opposite of what it returns now). 
To maintain the current behavior, use `saved_change_to_attribute?` instead. 

My code looks like this 我的代码看起来像这样

class MyClass
  before_valiadtion :my_method

  def my_method
    if name_changed?
      ...
    end
  end
end

I do not understand exactly the deprecation warning. 我不完全理解弃用警告。 If I use saved_change_to_name instead it will check it after it is saved but this is before validation callback. 如果我使用的是saved_change_to_name它会在保存后检查它,但这是在验证回调之前。

I also noticed that if I change the name_changed? 我还注意到,如果我更改name_changed? to saved_change_to_name my specs are not passing. saved_change_to_name我的规格没有通过。

I'm having a hard time understanding what the appropriate way to this should be. 我很难理解适当的方法应该是什么。 It seems to me that how it was before it was doing the job okay, not fully understand the reason behind this change and how I should avoid these deprecation warnings. 在我看来,在它完成工作之前是怎么回事,没有完全理解这种变化背后的原因以及我应该如何避免这些弃用警告。

There are new names for these methods that more clearly express wether you're looking for a change that has just been saved , or one that is about to be saved . 这些方法新的名称,可以更清楚地表达您正在寻找刚刚保存的更改,或者即将 保存的更改。

In your case, the latter one, you're supposed to use will_save_change_to_attribute?(:name) . 在你的情况下,后者,你应该使用will_save_change_to_attribute?(:name)

The deprecation warning is really misleading as it only mentions the first case, and assumes it must occur in an after callback. 弃用警告确实具有误导性,因为它只提到第一种情况,并假设它必须在后回调中发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “在回调之后``attribute_changed?`的行为”在基本的`save`之后 - “The behavior of `attribute_changed?` inside of after callbacks” After a basic `save` Rails:attribute_changed? - Rails: attribute_changed? 在 Rails 5.1 之后,attribute_changed 是如何变化的? 更改“验证”回调? - In post Rails 5.1, how does attribute_changed? chage for "validate" callbacks? 检查 attribute_changed 了吗? 在 ActiveModel::EachValidator 中 - Checking if attribute_changed? in ActiveModel::EachValidator 测试attribute_changed? VS. previous_changes检查特定属性是否更改(Rails),行为是否不同 - Testing attribute_changed? VS. previous_changes in checking if a specific attribute changed (Rails), behaves differently rails helper方法如何:attribute_changed? 在Rails 3.2.15中有效吗? - How does rails helper method :attribute_changed? works in Rails 3.2.15? Ruby在轨道上。 回调,指定更改的属性 - Ruby on rails. Callbacks, specifying changed attribute attr_accessors只是属性更改的触发器回调吗? - attr_accessors are only attribute changed trigger callbacks? 更改虚拟属性时如何关闭回调? - How to turn off callbacks when changing virtual attribute? 在 Rails 中提交后检查属性是否更改 - Checking if an attribute changed after commit in Rails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM