Understanding after_update callback in Rails 4

前端 未结 2 2040
傲寒
傲寒 2020-12-31 17:21

I have a Rails object with after_update callback that sends a record to a queue. And the problem is that I noticed sometimes the queue is being processed faster

相关标签:
2条回答
  • 2020-12-31 18:11

    If you consult the Rails documentation you will find a lot of callbacks you can use. The best for this job might be "after_commit":

    This is straight from the Rails Docs (link at the bottom)

    3.1 Creating an Object
    
    before_validation
    after_validation
    before_save
    around_save
    before_create
    around_create
    after_create
    after_save
    after_commit/after_rollback
    
    3.2 Updating an Object
    
    before_validation
    after_validation
    before_save
    around_save
    before_update
    around_update
    after_update
    after_save
    after_commit/after_rollback
    
    3.3 Destroying an Object
    
    before_destroy
    around_destroy
    after_destroy
    

    Rails DOcs: http://guides.rubyonrails.org/active_record_callbacks.html

    0 讨论(0)
  • 2020-12-31 18:15

    after_save, after_create, after_update are called within the transaction block, so they will be executed before executing the SQL statement.

    If you want to do something when the statement execution is completed, you should use after_commit callback.

    0 讨论(0)
提交回复
热议问题