17

I've just upgraded from Rails 5.0.0 to 5.1.1 and started getting a ton of deprecation warnings like this:

DEPRECATION WARNING: The behavior of changed_attributes 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.transform_values(&:first) instead.

and this:

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.

I don't use those methods explicitely anywhere in my project and the warnings are pointing mostly at create and update calls on my models.

I believe it has something to do with my validations and after_update and after_create callbacks where I use confitions like if: { author_id_changed? } but I have no idea what to do with them.

I also believe the warning is related to this massive update to ActiveRecord.

Would appreciate any hand you can give with this.

UPD

This article helped alot!

Almaron
  • 4,127
  • 6
  • 26
  • 48

4 Answers4

16

Well, got around everything by running bundle update and updating the gems and also following this article and changing attribute_changed? calls in after_ callbacks (but not in before_ callbacks and validations) and switching from attribute_was to attribute_before_last_save.

Almaron
  • 4,127
  • 6
  • 26
  • 48
8

For After callbacks, you can use saved_change_to_attribute?

For Before callbacks and validations, you can use will_save_change_to_attribute?

I hope this information will help!

deepak
  • 198
  • 2
  • 5
  • 1
    `will_save_change_to_attribute` is what i needed for my before callback, thanks – Raj Aug 14 '19 at 21:59
5

I've done an upgrade to Rails 5.1.6 and have the same DEPRECATION warnings. If ever anyone still wants to solve this warning. Here are the steps I took:

Search all of your*_changed?

Changed this:

if name_changed?
...
if user_id_changed?

To this if it is inside after_* (after_save, after_commit, after_update, etc.) blocks:

if saved_change_to_name?
...
if saved_change_to_user_id?

AND to this if it is inside before_* (before_save, before_commit, before_update, etc.) blocks:

if will_save_change_to_name?
...
if will_save_change_to_user_id?

On my own opinion, this is quite tricky thing to change since we've been used to attribute_changed?. But change is good. The syntax also made more sense now.

3

You can change author_id_changed? to saved_change_to_author_id?

akaspick
  • 1,541
  • 19
  • 17
  • Your answer below is correct for validations. The version I mentioned doesn't work in validations because the data hasn't actually been saved yet. – akaspick Jun 20 '17 at 18:49