弃用警告示例

时间:2017-08-28 00:05:49

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-5 deprecation-warning

我收到以下弃用警告:

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. 

代码:

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_mailer.send(notification, self, *args).deliver_later
  end
end

有人可以通过示例向我解释弃用警告吗?我不确定我是否正确理解The new return value will reflect the behavior of calling the method after "save" returned

的含义

我现在可以简单地用changed?替换saved_changes?吗?感谢

1 个答案:

答案 0 :(得分:0)

据我所知,它的工作原理如下。现在#changed?在回调后返回false,直到您更改属性为止。因此,记录上的#changed?在回调之后表现为好像刚刚通过#find获取记录(或者,如消息所示,如果您在调用#save后有记录) 。 #saved_changes?回答了问题:对#save的最后一次调用是否有任何变化?所以在我能想到的情况下你可以在回调之后安全地切换到这个方法。