新手问题 在 after_commit 里如何判断当前操作是通过 [:create, :destroy, :update] 中的哪一个进来的?

jmmbite · 2018年07月12日 · 最后由 theblock24block 回复于 2018年07月12日 · 1790 次阅读
after_commit :do_something, on: [:create, :update, :destroy]
def do_something
  # 在这里是否可以知晓,是 ACTIONS = [:create, :destroy, :update] 中的哪个触发的吗?
end

参考如下:

# https://github.com/rails/rails/blob/master/activerecord/lib/active_record/transactions.rb

# Determine if a transaction included an action for :create, :update, or :destroy. Used in filtering callbacks.
def transaction_include_any_action?(actions)
  actions.any? do |action|
    case action
    when :create
      persisted? && @_new_record_before_last_commit
    when :update
      !(@_new_record_before_last_commit || destroyed?) && _trigger_update_callback
    when :destroy
      _trigger_destroy_callback
    end
  end
end

分三次配置?

do_something_on_create[update, destroy]

pynix 回复

就是因为不想分 3 次😂

jmmbite 关闭了讨论。 07月12日 20:55
需要 登录 后方可回复, 如果你还没有账号请 注册新账号