Let's say I have:
class Department < ActiveRecord::Base
has_many :employees
end
class Employee < ActiveRecord::Base
belongs_to :department
end
If I destroy a department that has employees, the employees department_id becomes invalid and referential integrity brakes. I know there's :dependent => :nullify and :destroy, but there's nothing to stop me of deleting departments unless I specifically deassociate it's employees.
So I added the :protected option.
class Department < ActiveRecord::Base
has_many :employees, :dependent => :protect
end
It will raise a ReferentialIntegrityProtectionError whenever you try to destroy a Department whose employees.count > 0.
I'm attaching the patch with unit test included.
What do you think?
Diego