相关疑难解决方法(0)

在after_commit,:on =>:create中更新属性时防止无限循环

当我需要在a期间更新属性时,我创建了一个无限回调循环after_commit, :on => :create.但是,只有在我需要在此回调期间更新对象的属性时才会发生这种情况.有办法防止这种情况吗?我可以以某种方式强制模型重新加载自己,因此它知道它正在执行更新而不是创建吗?

class Doc < ActiveRecord::Base
  after_commit :generate, :on => :create

  ...

  def generate
    # some logic here that requires this model to be saved in the db

    self.update_attributes(:filename => filename) # Infinite loop begins here.
  end
end
Run Code Online (Sandbox Code Playgroud)

activerecord ruby-on-rails callback ruby-on-rails-4

10
推荐指数
1
解决办法
4227
查看次数