dimanche 16 juillet 2017

changed? depreaction Rails 5.1.2

Devise before it saves the record, it checks if attributes changed and if so it performs special actions:

def send_devise_notification(notification, *args)
  # If the record is new or changed then delay the
  # delivery until the after_commit callback otherwise
  # send now because after_commit will not be called.

  if new_record? || changed?
    pending_notifications << [notification, args]
  else
    # Devise: send emails with background job
    devise_mailer.send(notification, self, *args).deliver_later
  end
end

http://ift.tt/2v65vlG

The following line gives me an depreaction now:

if new_record? || changed? 

DEPRECATION WARNING: The behavior of '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_changes?' instead.

When I use saved_changes? instead of changed? the code won't work correctly anymore, because in this step the record is not yet saved

e.g.

user.email = "hello@example.com"
user.changed? => true
user.saved_changes? => false

Which method should I use instead? How can I prevent the depreaction warning? Thanks

Aucun commentaire:

Enregistrer un commentaire