Skip to content

Instantly share code, notes, and snippets.

@andrewroth
Created February 2, 2010 15:44
Show Gist options
  • Save andrewroth/292755 to your computer and use it in GitHub Desktop.
Save andrewroth/292755 to your computer and use it in GitHub Desktop.
module ActiveRecord
class Base
def before_destroy
associations_with_data.each { |assoc|
errors.add(:assoc, "association has data")
}
end
def associations_with_data
associations_with_data = []
self.class.reflect_on_all_associations.each { |assoc|
associations_with_data << assoc.name if self.send(assoc.name).present?
}
return associations_with_data
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment