Skip to content

Instantly share code, notes, and snippets.

@shuber
Forked from jondahl/has_many :dependent => :restrict
Created August 28, 2009 21:19
Show Gist options
  • Save shuber/177254 to your computer and use it in GitHub Desktop.
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
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