1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Fixed an error triggered by a reload followed by a foreign key assignment.

Signed-off-by: Michael Koziarski <michael@koziarski.com>
This commit is contained in:
Nathaniel Talbott 2008-09-19 16:42:18 -04:00 committed by Michael Koziarski
parent 79f55de9c5
commit 9d7f186f74
2 changed files with 13 additions and 1 deletions

View file

@ -1268,7 +1268,11 @@ module ActiveRecord
if association_proxy_class == BelongsToAssociation
define_method("#{reflection.primary_key_name}=") do |target_id|
instance_variable_get(ivar).reset if instance_variable_defined?(ivar)
if instance_variable_defined?(ivar)
if association = instance_variable_get(ivar)
association.reset
end
end
write_attribute(reflection.primary_key_name, target_id)
end
end

View file

@ -194,6 +194,14 @@ class AssociationProxyTest < ActiveRecord::TestCase
assert_equal david, welcome.author
end
def test_assigning_association_id_after_reload
welcome = posts(:welcome)
welcome.reload
assert_nothing_raised do
welcome.author_id = authors(:david).id
end
end
def test_reload_returns_assocition
david = developers(:david)
assert_nothing_raised do