Created
January 31, 2012 14:42
-
-
Save spalladino/1710839 to your computer and use it in GitHub Desktop.
How to check if object can be destroyed if it has dependent restrict associations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ActiveRecord::Base | |
def can_destroy? | |
self.class.reflect_on_all_associations.all? do |assoc| | |
assoc.options[:dependent] != :restrict || (assoc.macro == :has_one && self.send(assoc.name).nil?) || (assoc.macro == :has_many && self.send(assoc.name).empty?) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Rails 7 🎉 👍
Thanks everyone!