【问题标题】:Example for deprecation warning弃用警告示例
【发布时间】:2017-08-28 00:05:49
【问题描述】:

我收到以下弃用警告:

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? 吗?谢谢

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-11
      • 2021-07-12
      • 2017-08-22
      • 2011-09-29
      • 2018-09-08
      相关资源
      最近更新 更多