Permalink
Browse files

Merge [6804] to stable: belongs_to assignment creates a new proxy rat…

…her than modifying its target in-place. References #8412.

git-svn-id: http://svn-commit.rubyonrails.org/rails/branches/1-2-stable@6805 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 099c206 commit 3ad1a9855685b11d1a6c4e78354a23ff125729d9 @jeremy jeremy committed May 22, 2007
View
@@ -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]
@@ -916,7 +916,7 @@ def association_accessor_methods(reflection, association_proxy_class)
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
@@ -926,10 +926,7 @@ def association_accessor_methods(reflection, association_proxy_class)
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|
@@ -1007,7 +1007,20 @@ def test_natural_assignment
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")

0 comments on commit 3ad1a98

Please sign in to comment.