From 7267db58906434da54e0f50122b5e8b704b1c953 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 6 Mar 2005 13:40:21 +0000 Subject: Added destruction of dependent objects in has_one associations when a new assignment happens #742 [mindel] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@843 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../lib/active_record/associations/has_one_association.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'activerecord/lib/active_record/associations/has_one_association.rb') diff --git a/activerecord/lib/active_record/associations/has_one_association.rb b/activerecord/lib/active_record/associations/has_one_association.rb index 30704b6f7e..e566089013 100644 --- a/activerecord/lib/active_record/associations/has_one_association.rb +++ b/activerecord/lib/active_record/associations/has_one_association.rb @@ -10,8 +10,13 @@ module ActiveRecord def replace(obj, dont_save = false) load_target unless @target.nil? - @target[@association_class_primary_key_name] = nil - @target.save unless @owner.new_record? + if dependent? && !dont_save + @target.destroy unless @target.new_record? + @owner.clear_association_cache + else + @target[@association_class_primary_key_name] = nil + @target.save unless @owner.new_record? + end end if obj.nil? -- cgit v1.2.3