From 3053f52671164530b811238f86954cb96f6ebc16 Mon Sep 17 00:00:00 2001 From: Paco Guzman Date: Sun, 29 Jan 2012 15:24:03 +0100 Subject: Same method for has_many and has_one associations --- .../lib/active_record/associations/builder/association.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'activerecord/lib/active_record/associations/builder/association.rb') diff --git a/activerecord/lib/active_record/associations/builder/association.rb b/activerecord/lib/active_record/associations/builder/association.rb index 54e51b7f44..45d3f37089 100644 --- a/activerecord/lib/active_record/associations/builder/association.rb +++ b/activerecord/lib/active_record/associations/builder/association.rb @@ -65,5 +65,20 @@ module ActiveRecord::Associations::Builder ActiveSupport::Deprecation.warn msg end end + + def define_restrict_dependency_method + name = self.name + mixin.redefine_method(dependency_method_name) do + # has_many or has_one associations + if send(name).respond_to?(:exists?) ? send(name).exists? : !send(name).nil? + if dependent_restrict_raises? + raise ActiveRecord::DeleteRestrictionError.new(name) + else + errors.add(:base, :restrict_dependent_destroy, :model => name) + return false + end + end + end + end end end -- cgit v1.2.3