Skip to content

Instantly share code, notes, and snippets.

@gustaflindqvist
Forked from spalladino/can_destroy.rb
Created June 19, 2014 13:04
Show Gist options
  • Save gustaflindqvist/259dfc7c8a19ec8f6320 to your computer and use it in GitHub Desktop.
Save gustaflindqvist/259dfc7c8a19ec8f6320 to your computer and use it in GitHub Desktop.
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