aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG2
-rwxr-xr-xactiverecord/lib/active_record/associations.rb5
-rwxr-xr-xactiverecord/test/associations_test.rb15
3 files changed, 17 insertions, 5 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 5a9b781b72..7d8d38ee8d 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* belongs_to assignment creates a new proxy rather than modifying its target in-place. #8412 [mmangino@elevatedrails.com]
+
* Fix column type detection while loading fixtures. Closes #7987 [roderickvd]
* Document deep eager includes. #6267 [Josh Susser, Dan Manges]
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index b13c34d59c..c72024d5f3 100755
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -985,7 +985,7 @@ module ActiveRecord
define_method("#{reflection.name}=") do |new_value|
association = instance_variable_get("@#{reflection.name}")
- if association.nil?
+ if association.nil? || association.target != new_value
association = association_proxy_class.new(self, reflection)
end
@@ -995,10 +995,7 @@ module ActiveRecord
instance_variable_set("@#{reflection.name}", association)
else
instance_variable_set("@#{reflection.name}", nil)
- return nil
end
-
- association
end
define_method("set_#{reflection.name}_target") do |target|
diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb
index 8f60fc95f1..03c344de7c 100755
--- a/activerecord/test/associations_test.rb
+++ b/activerecord/test/associations_test.rb
@@ -1039,7 +1039,20 @@ class BelongsToAssociationsTest < Test::Unit::TestCase
citibank.firm = apple
assert_equal apple.id, citibank.firm_id
end
-
+
+ def test_no_unexpected_aliasing
+ first_firm = companies(:first_firm)
+ another_firm = companies(:another_firm)
+
+ citibank = Account.create("credit_limit" => 10)
+ citibank.firm = first_firm
+ original_proxy = citibank.firm
+ citibank.firm = another_firm
+
+ assert_equal first_firm.object_id, original_proxy.object_id
+ assert_equal another_firm.object_id, citibank.firm.object_id
+ end
+
def test_creating_the_belonging_object
citibank = Account.create("credit_limit" => 10)
apple = citibank.create_firm("name" => "Apple")