导轨 5 弃用警告"The behavior of `attribute_change`"



我正在尝试从4.2升级到rails 5.1.0,并在运行我的RSPEC测试套件时获得此错误。

DEPRECATION WARNING: The behavior of `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_changes.keys` instead. (called from has_changes? at /Users/djohnson/projects/EvantaAccessAPI/app/models/user.rb:280)

我已经检查了一些类似的Stackoverflow问题,但似乎没有一个完全符合我的情况。我正在使用Elasticsearch和Chewy,并在我的用户中使用此行。RB模型。

update_index('users') { self if has_changes? }

在下面调用has_changes?方法:

def has_changes?
  changes.empty? || (changes.keys & %w(first_name last_name title organization_name)).present?
end

重构维持现有功能并删除这些弃用警告的最佳方法是什么?

谢谢!

我将方法的身体更改为

saved_changes.empty? || (saved_changes.keys & %w(first_name last_name title organization_name)).present?

似乎工作相同,但没有百万折旧警告。

changed?似乎也是如此,现在更喜欢saved_changes?

最新更新