Almaron
Almaron

Reputation: 4147

Rails 5.1.1 deprecation warning changed_attributes

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!

Upvotes: 17

Views: 11329

Answers (4)

Tophs Celestial
Tophs Celestial

Reputation: 51

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.

Upvotes: 5

deepak
deepak

Reputation: 198

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!

Upvotes: 8

Almaron
Almaron

Reputation: 4147

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.

Upvotes: 16

akaspick
akaspick

Reputation: 1697

You can change author_id_changed? to saved_change_to_author_id?

Upvotes: 3

Related Questions