【发布时间】:2014-03-21 19:00:26
【问题描述】:
当我需要在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
【问题讨论】:
-
能否分享一下服务器生成的日志。另外,如果您分享完整的
generate方法,那就太好了。 -
我也遇到过同样的问题,我认为是bug。
after_commit应该只触发on: :create作为声明,但实际上它被触发了,即使它被更新了。
标签: ruby-on-rails activerecord ruby-on-rails-4 callback