Skip to content

Commit

Permalink
Merge [6804] to stable: belongs_to assignment creates a new proxy rat…
Browse files Browse the repository at this point in the history
…her than modifying its target in-place. References rails#8412.

git-svn-id: http://svn-commit.rubyonrails.org/rails/branches/1-2-stable@6805 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
jeremy committed May 22, 2007
1 parent 099c206 commit 3ad1a98
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
2 changes: 2 additions & 0 deletions 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]
Expand Down
5 changes: 1 addition & 4 deletions activerecord/lib/active_record/associations.rb
Expand Up @@ -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

Expand All @@ -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|
Expand Down
15 changes: 14 additions & 1 deletion activerecord/test/associations_test.rb
Expand Up @@ -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")
Expand Down

0 comments on commit 3ad1a98

Please sign in to comment.