Menu

Toshimaru's Blog

Use saved_change_to_attribute? instead of attribute_changed? as of Rails 5.1

As of Ruby on Rails 5.1, attribute_changed? ActiveRecord method have been deprecated.

The deprecation message:

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.

As the messages says, using saved_change_to_attribute? instead of attribute_changed? is recommended.

Use saved_change_to_attribute? instead of attribute_changed?

Let’s say you write code like this:

class Post < ApplicationRecord
  after_update :deprecation_after_update

  def deprecation_after_update
    # Post has title column
    if title_changed?
      # ...
    end
  end
end

Then, you need to rewrite it like this:

class Post < ApplicationRecord
  after_update :deprecation_after_update

  def deprecation_after_update
    if saved_change_to_attribute?(:title)
      # ...
    end
  end
end

Use attribute_before_last_save instead of attribute_was

Also, attribute_was is deprecated in Rails 5.1. which is one of methods in ActiveModel::Dirty.

The deprecation message is:

DEPRECATION WARNING: The behavior of `attribute_was` 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 `attribute_before_last_save` instead.

As message says, we need to replace attribute_was with attribute_before_last_save.

For example:

-title_was
+attribute_before_last_save(:title)

Why this change needed?

sgrif explains why on GitHub issue: Deprecate the behavior of AR::Dirty inside of after_(create|update|save) callbacks by sgrif · Pull Request #25337 · rails/rails

Conversion Table (before_update/after_update)

Rails 5.0- Rails 5.1+
(before_update)
Rails 5.1+
(after_update)
attribute_changed? will_save_change_to_attribute? saved_change_to_attribute?
attribute_change attribute_change_to_be_saved saved_change_to_attribute
attribute_was attribute_in_database attribute_before_last_save
changes changes_to_save saved_changes
changed? has_changes_to_save? saved_changes?
changed changed_attribute_names_to_save saved_changes.keys
changed_attributes attributes_in_database saved_changes.transform_values(&:first)

ref. [Japanese] Rails 5.1 で attribute_was, attribute_change, attribute_changed?, changed?, changed 等が DEPRECATION WARNING - Qiita

Reference

Load more