I had a model which wasn’t cleaning up the dependent models, even though the :dependent => :destroy attribute was set on the association:
class PerformancePlan < ActiveRecord::Base has_many :goals, dependent: :destroy validates_associated :goals end class Goal < ActiveRecord::Base belongs_to :plan, class_name: 'PerformancePlan' end
Luckily, I’d written a spec to test that the goals were being cleaned up correctly:
it "must destroy the goals when deleted" do
plan = FactoryGirl.create :performance_plan, :with_goals, goal_count: 3
Goal.count.should eq(3)
expect {
plan.destroy
}.to change(Goal, :count).by(-3)
end
The log showed now indication of any errors or even an attempt to destroy the goals. To get this working, I needed to remove the validates_associated property:
class PerformancePlan < ActiveRecord::Base has_many :goals, dependent: :destroy end class Goal < ActiveRecord::Base belongs_to :plan, class_name: 'PerformancePlan' end