Rails:避免递归破坏回调

时间:2014-11-24 14:27:49

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

我有两种模式:

class Car < ActiveRecord::Base
  has_many  :adverts, :dependent => :destroy
end

class Advert < ActiveRecord::Base
  belongs_to  :car

  # Destroy the car, if there is no more adverts left
  after_destroy do
    self.car.destroy unless self.car.adverts.exists?
  end
end

现在调用advert.destroy时效果很好,但是当调用car.destroy时,事情会以递归循环结束!

PS。 rails 4.1.8

0 个答案:

没有答案