belongs_to assignment creates a new proxy rather than modifying its target in-place. Closes #8412.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6804 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
parent
8ba22a690c
commit
4afd6c9f0a
|
@ -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]
|
||||
|
|
|
@ -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|
|
||||
|
|
|
@ -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")
|
||||
|
|
Loading…
Reference in New Issue