From ec9a2b707233094aa05b7b18a6a6e1bd37a35279 Mon Sep 17 00:00:00 2001 From: Nathaniel Talbott Date: Fri, 19 Sep 2008 16:42:18 -0400 Subject: [PATCH] Fixed an error triggered by a reload followed by a foreign key assignment. --- activerecord/lib/active_record/associations.rb | 6 +++++- activerecord/test/cases/associations_test.rb | 8 ++++++++ 2 files changed, 13 insertions(+), 1 deletions(-) diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 5d91315..d7aa4bf 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -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 diff --git a/activerecord/test/cases/associations_test.rb b/activerecord/test/cases/associations_test.rb index 2050d16..778f18c 100644 --- a/activerecord/test/cases/associations_test.rb +++ b/activerecord/test/cases/associations_test.rb @@ -193,6 +193,14 @@ class AssociationProxyTest < ActiveRecord::TestCase assert_nil author_assoc.target 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) -- 1.5.5.3