after_commit:on => :以类似观察者的方式摧毁

时间:2012-08-07 09:42:36

标签: ruby-on-rails activerecord callback

我想做以下事情:

after_commit :on => :destroy do
  Advert.clean_total_active_cache
end

但是以类似观察者的方式:

class AdvertObserver < ActiveRecord::Observer
  def after_commit advert
    Advert.clean_total_active_cache
  end
end

这是“:on =&gt;:destroy”,这会给我带来麻烦,我该如何指定?

1 个答案:

答案 0 :(得分:0)

您可以检查记录是否未被保留和冻结:

class AdvertObserver < ActiveRecord::Observer
  def after_commit advert
    if not advert.persisted? and advert.frozen?
      Advert.clean_total_active_cache
    end
  end
end