Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed an error triggered by a reload followed by a foreign key assign…

…ment.

Signed-off-by: Michael Koziarski <michael@koziarski.com>
  • Loading branch information...
commit 9d7f186f746f38367851355fcd301ac24d6f6a51 1 parent 79f55de
@ntalbott ntalbott authored NZKoz committed
View
6 activerecord/lib/active_record/associations.rb
@@ -1268,7 +1268,11 @@ def association_accessor_methods(reflection, association_proxy_class)
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
8 activerecord/test/cases/associations_test.rb
@@ -194,6 +194,14 @@ def test_reset_loads_association_next_time
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

0 comments on commit 9d7f186

Please sign in to comment.
Something went wrong with that request. Please try again.