【问题标题】:Understanding after_update callback in Rails 4了解 Rails 4 中的 after_update 回调
【发布时间】:2017-04-09 19:09:44
【问题描述】:

我有一个带有after_update 回调的Rails 对象,它将记录发送到队列。问题是我注意到有时队列的处理速度比对象的实际更新速度要快。

我的问题:after_update 不是在对象更新结束后调用,而是在它开始时调用?如果我只想更新成功后对其进行操作,我需要什么回调?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 callback


    【解决方案1】:

    after_saveafter_createafter_update是在事务块中调用的,所以会在执行SQL语句之前执行。

    如果你想在语句执行完成后做一些事情,你应该使用after_commit回调。

    【讨论】:

      【解决方案2】:

      如果您查阅 Rails 文档,您会发现很多可以使用的回调。最适合这项工作的可能是“after_commit”:

      这直接来自 Rails 文档(链接在底部)

      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 文档:http://guides.rubyonrails.org/active_record_callbacks.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-04
        • 2016-12-22
        • 2014-06-26
        相关资源
        最近更新 更多