【发布时间】:2017-07-16 10:43:50
【问题描述】:
在保存记录之前进行设计,它会检查属性是否更改,如果更改则执行特殊操作:
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
以下行现在给了我一个弃用:
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.
当我使用saved_changes? 而不是changed? 时,代码将无法正常工作,因为在此步骤中尚未保存记录
例如
user.email = "hello@example.com"
user.changed? => true
user.saved_changes? => false
我应该改用哪种方法?如何防止弃用警告?谢谢
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-3 devise