From d665f91d412a29d7bd93db38b8fdbc07ed50c510 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Tue, 25 Sep 2012 13:20:26 -0700 Subject: Fix destructive side effects from marshaling an association caused by 65843e1acc0c8d285ff79f8c9c49d4d1215440be --- activerecord/lib/active_record/associations/association.rb | 7 ++----- activerecord/test/cases/associations/extension_test.rb | 7 +++++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/activerecord/lib/active_record/associations/association.rb b/activerecord/lib/active_record/associations/association.rb index 495f0cde59..ba75c8be41 100644 --- a/activerecord/lib/active_record/associations/association.rb +++ b/activerecord/lib/active_record/associations/association.rb @@ -154,11 +154,8 @@ module ActiveRecord # We can't dump @reflection since it contains the scope proc def marshal_dump - reflection = @reflection - @reflection = nil - - ivars = instance_variables.map { |name| [name, instance_variable_get(name)] } - [reflection.name, ivars] + ivars = (instance_variables - [:@reflection]).map { |name| [name, instance_variable_get(name)] } + [@reflection.name, ivars] end def marshal_load(data) diff --git a/activerecord/test/cases/associations/extension_test.rb b/activerecord/test/cases/associations/extension_test.rb index bd5a426ca8..da767a2a7e 100644 --- a/activerecord/test/cases/associations/extension_test.rb +++ b/activerecord/test/cases/associations/extension_test.rb @@ -40,9 +40,12 @@ class AssociationsExtensionsTest < ActiveRecord::TestCase assert_equal projects(:action_controller), david.projects.find_most_recent marshalled = Marshal.dump(david) - david = Marshal.load(marshalled) - assert_equal projects(:action_controller), david.projects.find_most_recent + # Marshaling an association shouldn't make it unusable by wiping its reflection. + assert_not_nil david.association(:projects).reflection + + david_too = Marshal.load(marshalled) + assert_equal projects(:action_controller), david_too.projects.find_most_recent end def test_marshalling_named_extensions -- cgit v1.2.3