简体   繁体   中英

Rails 5 Deprecation Warning “The behavior of `attribute_change`”

I'm attempting to upgrade to Rails 5.1.0 from 4.2 and getting this error while running my Rspec test suite.

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)

I've checked out a few similar Stackoverflow questions but none seemed to quite match my scenario. I'm using elasticsearch and chewy and have this line in my user.rb model.

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

Which calls the has_changes? method below:

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

What is the best way to refactor this to maintain existing functionality and remove these deprecation warnings?

Thanks!

I changed the method's body to

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

and it seems to be working the same but without the million deprecation warnings.

It seems the same goes for changed? which now prefers to be called saved_changes?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM