簡體   English   中英

Rails ActiveRecord條件回調問題

[英]Rails ActiveRecord conditional callback issue

紅寶石2.1.8導軌3.2.18

我正在嘗試在僅在特定屬性已更改時保存記錄時運行回調。 例如

before_save :do_the_thing, if: :my_attr_changed?

但是,當我更改my_attr並保存時, do_the_thing不會被調用。 然而,如果我做同樣的事情,但是:

before_save :do_the_thing

def do_the_thing
  puts my_attr_changed?
end

它在日志中輸出“true”。 在這里相當困惑。 任何幫助贊賞。 謝謝。

把它移到lambda里面吧

before_save :do_the_thing, if: -> { my_attr_changed? }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM