From a0be389d39b790e0625339251d2674b8250b16b1 Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Sun, 2 Jan 2011 14:28:53 +0000 Subject: Allow assignment on has_one :through where the owner is a new record [#5137 state:resolved] This required changing the code to keep the association proxy for a belongs_to around, despite its target being nil. Which in turn required various changes to the way that stale target checking is handled, in order to support various edge cases (loaded target is nil then foreign key added, foreign key is changed and then changed back, etc). A side effect is that the code is nicer and more succinct. Note that I am removing test_no_unexpected_aliasing since that is basically checking that the proxy for a belongs_to *does* change, which is the exact opposite of the intention of this commit. Also adding various tests for various edge cases and related things. Phew, long commit message! --- activerecord/lib/active_record/associations/has_one_association.rb | 4 +--- 1 file changed, 1 insertion(+), 3 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 12ccb3af8a..7bbeb8829a 100644 --- a/activerecord/lib/active_record/associations/has_one_association.rb +++ b/activerecord/lib/active_record/associations/has_one_association.rb @@ -33,10 +33,8 @@ module ActiveRecord case @reflection.options[:dependent] when :delete @target.delete if @target.persisted? - @owner.clear_association_cache when :destroy @target.destroy if @target.persisted? - @owner.clear_association_cache when :nullify @target[@reflection.foreign_key] = nil @target.save if @owner.persisted? && @target.persisted? @@ -56,7 +54,7 @@ module ActiveRecord end set_inverse_instance(obj) - @loaded = true + loaded unless !@owner.persisted? || obj.nil? || dont_save return (obj.save ? self : false) -- cgit v1.2.3