Rails 5弃用警告“attribute_change`的行为”

时间:2018-04-25 15:27:28

标签: ruby-on-rails callback ruby-on-rails-5 ruby-on-rails-5.1 deprecation-warning

我正在尝试从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,并在我的user.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

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

谢谢!

1 个答案:

答案 0 :(得分:0)

我将方法的主体更改为

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

它似乎工作相同,但没有百万个弃用警告。

对于changed?来说似乎也是如此,现在更喜欢称之为saved_changes?