Rails 5.2 changes in callbacks

In version 5.1 you may see deprecation warnings in after_save callbacks (related to changes in ActiveRecord::Dirty module).

But since 5.2 these changes were applied.

For examples, I will use Rails 4.2.11 and Rails 5.2.3 and model User with email attribute. Let's do:

 u = User.new(email: '[email protected]') u.save u.email = '[email protected]' u.save 

and look at after_save callback in time of last save.

1. attribute_changed?

Rails 4

 > email_changed? => true 

Rails 5.2

 > email_changed? => false 

but you can use saved_changes?

 > saved_change_to_email? => true 

2. changed?

Rails 4

 > changed? => true 

Rails 5.2

 > changed? => false 

but you can use saved_changes?

 > saved_changes? => true 

3. changes

Rails 4

 > changes => {"email"=>["[email protected]", "[email protected]"]} 

Rails 5.2

 > changes => {} 

but you can use saved_changes

 > saved_changes => {"email"=>["[email protected]", "[email protected]"]} 

4. previous_changes

Rails 4

 > previous_changes => {"email"=>[nil, "[email protected]"]} 

Rails 5.2

Now, this method returns the changes that were just saved (like saved_changes).

 > previous_changes => {"email"=>["[email protected]", "[email protected]"]} 

this method has no replacement.

Do you have a project for us?Get a free quote