I have two classes: Parent and Child with
Child:
belongs_to :parent
and
Parent
has_many :children, :dependent
In Rails 4 you can do the following:
class Parent < AR::Base
has_many :children, dependent: :destroy
end
class Child < AR::Base
belongs_to :parent
before_destroy :check_destroy_allowed, unless: :destroyed_by_association
private
def check_destroy_allowed
# some condition that returns true or false
end
end
This way, when calling destroy
directly on a child, the check_destroy_allowed
callback will be run, but when you call destroy
on the parent, it won't.