我的模型中有这段代码
after_create :publish_properties
after_update :publish_properties
before_destroy :publish_properties
在 publish_properties
内部,我怎么知道哪个是动作 (update, create or delete
) ?
回答1
您可以使用回调块语法 (https://guides.rubyonrails.org/active_record_callbacks.html#callback-registration) 以像这样的操作类型显式调用您的回调
after_create { publish_properties :create }
...
def publish_properties(action_type)
...
end
回答2
我想你想要 transaction_include_any_action
。您可以通过在函数中使用以下代码来获取操作名称来执行此操作
导轨 4+
self.transaction_include_any_action?([:<action_name>])
导轨 < 4
self.transaction_include_action?([:<action_name>])
在此操作中,名称可以是 :create、:destroy、:commit 等。