2017-07-16 9 views
0

工夫それがレコードを保存する前、属性が変更された場合には、チェックしている場合は、それは特別なアクションを実行:が変更されましたか? depreactionのRails 5.1.2

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://www.rubydoc.info/github/plataformatec/devise/Devise%2FModels%2FAuthenticatable%3Asend_devise_notification

次の行は、今私にdepreactionを与える:

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.

ときI Uこのステップではレコードがまだ保存されていないため、changed?の代わりにchanged?のコードを使用してください。

user.email = "[email protected]" 
user.changed? => true 
user.saved_changes? => false 

代わりにどの方法を使用しますか?どのようにして鎮圧の警告を防止できますか?ありがとう

答えて

1

このメッセージは、changed?がafter_createやafter_saveのようなコールバックの後で異なる動作をすることを意味しています。レコードがすでに保存されますので、あなたの代わりにsaved_changes?を使用することができ、それは、これらのコールバック

しかし、あなたはその後、changed?を残す before_save例えばコールバックの前にそれを使用したいと、それを交換しない場合ので、それは意志でうまく動作します以前と同じように正常に動作します

オブジェクトが保存されているかどうか気にしない場合。両方を確認するだけです。new_record? || saved_changes? || changed?

ユーザーのメールの変更に関する例については、デベロッパーは保存後に確認を送信するので、saved_changes?だけがうまくいくはずです!

関連する問題