-
-
Save shuber/177254 to your computer and use it in GitHub Desktop.
don't allow a record with has_many dependencies to be destroyed e.g. has_many dependent: :restrict
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
when :restrict do | |
method_name = "has_many_dependent_restrict_for_#{reflection.name}".to_sym | |
define_method(method_name) do | |
unless send(reflection.name).empty? | |
# change to named error... | |
raise ActiveRecord::StatementInvalid, "can't delete record because of dependent #{reflection.name}" | |
end | |
end | |
before_destroy method_name | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment