我有两种模式:
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