"The behavior of `attribute_changed?` inside of after callbacks" After a basic `save`

Viewed 361

This is my hook method called after after_create :set_after_create in a model.

  def set_after_create
    passphrase = "#{id}+#{Time.zone.now.to_date}#{Rails.application.secrets.secret_key_base}"
    update(
      ref: CryptLib.sha1(passphrase),
      token_key: CryptLib.sha1("#{passphrase}+#{Random.rand(20)}")
    )
  end

Now, I get a warning:

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. (called from set_after_create at /app/app/models/project.rb:330)

I don't understand the connection between the alert and the method.

PS: I get no warning using the update_columns method, but I don't think that's the right way.

Config: Rails 5.1.4 and all gems updated.

1 Answers

I think you are using attribute_changed? at some other place in the same model. Method attribute_changed? will deprecate in rails newer versions. So, instead of using attribute_changed?, use will_save_change_to_attribute? For more details see here .

Related