Rails 3: How to identify after_commit action in observers? (create/update/destroy)

前端 未结 9 1302
慢半拍i
慢半拍i 2021-01-30 02:17

I have an observer and I register an after_commit callback. How can I tell whether it was fired after create or update? I can tell an item was destroyed by asking

9条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-30 02:54

    Take a look at the test code: https://github.com/rails/rails/blob/master/activerecord/test/cases/transaction_callbacks_test.rb

    There you can find:

    after_commit(:on => :create)
    after_commit(:on => :update)
    after_commit(:on => :destroy)
    

    and

    after_rollback(:on => :create)
    after_rollback(:on => :update)
    after_rollback(:on => :destroy)
    

提交回复
热议问题