mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Try reloading model on class mismatch [#229 state:resolved]
Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
This commit is contained in:
parent
b437a7d34e
commit
7659fb6a2b
2 changed files with 21 additions and 1 deletions
|
@ -240,7 +240,7 @@ module ActiveRecord
|
|||
# the kind of the class of the associated objects. Meant to be used as
|
||||
# a sanity check when you are about to assign an associated record.
|
||||
def raise_on_type_mismatch(record)
|
||||
unless record.is_a?(@reflection.klass)
|
||||
unless record.is_a?(@reflection.klass) || record.is_a?(@reflection.class_name.constantize)
|
||||
message = "#{@reflection.class_name}(##{@reflection.klass.object_id}) expected, got #{record.class}(##{record.class.object_id})"
|
||||
raise ActiveRecord::AssociationTypeMismatch, message
|
||||
end
|
||||
|
|
20
activerecord/test/cases/reload_models_test.rb
Normal file
20
activerecord/test/cases/reload_models_test.rb
Normal file
|
@ -0,0 +1,20 @@
|
|||
require "cases/helper"
|
||||
require 'models/owner'
|
||||
require 'models/pet'
|
||||
|
||||
class ReloadModelsTest < ActiveRecord::TestCase
|
||||
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')
|
||||
assert_equal pet.owner, Owner.find_by_name('ashley')
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue