Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@andhapp
Forked from spalladino/can_destroy.rb
Created February 7, 2012 18:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andhapp/1761098 to your computer and use it in GitHub Desktop.
Save andhapp/1761098 to your computer and use it in GitHub Desktop.
How to check if object can be destroyed if it has dependent restrict associations
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