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

前端 未结 9 1301
慢半拍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:45

    You can change your event hook from after_commit to after_save, to capture all create and update events. You can then use:

    id_changed?
    

    ...helper in the observer. This will be true on create and false on an update.

提交回复
热议问题