From 4fc4c037f1432714986d274a0d1d3294f176556a Mon Sep 17 00:00:00 2001 From: Lawrence Pit Date: Wed, 21 May 2008 11:28:33 +1000 Subject: [PATCH] Fix for type mismatch after models are reloaded --- .../associations/association_proxy.rb | 4 +- .../lib/active_record/reload_models_test.rb | 28 ++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 activerecord/lib/active_record/reload_models_test.rb diff --git a/activerecord/lib/active_record/associations/association_proxy.rb b/activerecord/lib/active_record/associations/association_proxy.rb index ec16af3..2673638 100644 --- a/activerecord/lib/active_record/associations/association_proxy.rb +++ b/activerecord/lib/active_record/associations/association_proxy.rb @@ -209,8 +209,8 @@ module ActiveRecord end def raise_on_type_mismatch(record) - unless record.is_a?(@reflection.klass) - raise ActiveRecord::AssociationTypeMismatch, "#{@reflection.klass} expected, got #{record.class}" + unless record.is_a?(@reflection.klass) || record.is_a?(eval(@reflection.class_name)) + raise ActiveRecord::AssociationTypeMismatch, "#{@reflection.class_name} (class id=#{@reflection.klass.object_id}) expected, got #{record.class} (class id=#{record.class.object_id})" end end diff --git a/activerecord/lib/active_record/reload_models_test.rb b/activerecord/lib/active_record/reload_models_test.rb new file mode 100644 index 0000000..b6bdbe6 --- /dev/null +++ b/activerecord/lib/active_record/reload_models_test.rb @@ -0,0 +1,28 @@ +require "cases/helper" +require 'models/owner' +require 'models/pet' + +class ReloadModelsTest < ActiveRecord::TestCase + def setup + end + + def teardown + end + + def test_has_one_with_reload + # Reload the class Owner, simulating auto-reloading of model classes in a + # development environment. Note that meanwhile the class Pet is not + # reloaded, simulating a class that is present in a plugin. + Object.class_eval { remove_const :Owner } + Kernel.load(File.expand_path(File.join(File.dirname(__FILE__), "../models/owner.rb"))) + + pet = Pet.find_by_name('parrot') + pet.owner = Owner.find_by_name('ashley') + # After reload this assignment failed with the error message: + # ActiveRecord::AssociationTypeMismatch: Owner expected, got Owner + # See also http://dev.rubyonrails.org/ticket/8246 + + assert_equal pet.owner, Owner.find_by_name('ashley') + end + +end -- 1.5.5.1