From 331e20bc61dd00ff1d0265a313433addafde81c9 Mon Sep 17 00:00:00 2001 From: Lawrence Pit Date: Thu, 22 May 2008 07:49:43 +1000 Subject: [PATCH] reload_models_test --- activerecord/test/cases/reload_models_test.rb | 31 +++++++++++++++++++++++++ 1 files changed, 31 insertions(+), 0 deletions(-) create mode 100644 activerecord/test/cases/reload_models_test.rb diff --git a/activerecord/test/cases/reload_models_test.rb b/activerecord/test/cases/reload_models_test.rb new file mode 100644 index 0000000..59f1617 --- /dev/null +++ b/activerecord/test/cases/reload_models_test.rb @@ -0,0 +1,31 @@ +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 + pet = Pet.find_by_name('parrot') + pet.owner = Owner.find_by_name('ashley') + + # 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 \ No newline at end of file -- 1.5.5.1 From 60051475beac006e34923dc0e486779c09a8f00e Mon Sep 17 00:00:00 2001 From: Lawrence Pit Date: Thu, 22 May 2008 07:53:06 +1000 Subject: [PATCH] raise_on_type_mismatch --- .../associations/association_proxy.rb | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-) diff --git a/activerecord/lib/active_record/associations/association_proxy.rb b/activerecord/lib/active_record/associations/association_proxy.rb index 11c6424..d295692 100644 --- a/activerecord/lib/active_record/associations/association_proxy.rb +++ b/activerecord/lib/active_record/associations/association_proxy.rb @@ -209,7 +209,7 @@ module ActiveRecord end def raise_on_type_mismatch(record) - unless record.is_a?(@reflection.klass) + unless record.is_a?(@reflection.klass) || record.is_a?(eval(@reflection.class_name)) message = "#{@reflection.class_name}(##{@reflection.klass.object_id}) expected, got #{record.class}(##{record.class.object_id})" raise ActiveRecord::AssociationTypeMismatch, message end -- 1.5.5.1