Last active
January 24, 2019 09:05
-
-
Save HirschSt/9fd9cbfb9671334c791ce5911cdb2290 to your computer and use it in GitHub Desktop.
dependency check in foreign_links
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
def check_dependencies | |
msg = {} | |
self.class.reflect_on_all_associations.map{|e| e.name}.each do |assoc| | |
dependency_size = self.send(assoc).size rescue next | |
msg[assoc] = dependency_size if dependency_size > 0 | |
end | |
unless msg.empty? | |
linked_objects = "#{msg.map{|k,v| "#{k} #{v}"}.join(", ")}" | |
errors.add :base, %{The #{self.class} could not be deleted because it is used by | |
#{linked_objects} } | |
throw :abort | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment