Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails after回调中的'attribute_changed?'的行为将发生变化_Ruby On Rails - Fatal编程技术网

Ruby on rails after回调中的'attribute_changed?'的行为将发生变化

Ruby on rails after回调中的'attribute_changed?'的行为将发生变化,ruby-on-rails,Ruby On Rails,我刚刚将一个项目升级到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` r

我刚刚将一个项目升级到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. 
我的代码如下所示

class MyClass
  before_valiadtion :my_method

  def my_method
    if name_changed?
      ...
    end
  end
end
我不完全理解弃用警告。如果我使用
saved\u change\u to\u name
,它将在保存后进行检查,但这是在验证回调之前

我还注意到,如果我将
name\u changed?
更改为
saved\u change\u更改为\u name
我的规格没有通过

我很难理解什么才是合适的方法。在我看来,在它开始工作之前是怎样的,好吧,我还不完全理解这项更改背后的原因,以及我应该如何避免这些弃用警告。

有一个更清楚的表达,你是在寻找刚刚保存的更改,还是即将保存的更改

在后一种情况下,您应该使用
will\u save\u change\u to\u attribute?(:name)


弃用警告实际上是误导性的,因为它只提到了第一种情况,并假设它必须发生在后回调中。

您仍然看到这个问题吗?我自己也尝试过在验证之前用
回调来复制它,但是弃用警告没有出现(Rail 5.1.1)。