Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #8246: type_mismatch.patch

File type_mismatch.patch, 2.5 kB (added by lawrence, 3 months ago)
  • a/activerecord/lib/active_record/associations/association_proxy.rb

    old new  
    209209        end 
    210210 
    211211        def raise_on_type_mismatch(record) 
    212           unless record.is_a?(@reflection.klass) 
    213             raise ActiveRecord::AssociationTypeMismatch, "#{@reflection.klass} expected, got #{record.class}
     212          unless record.is_a?(@reflection.klass) || record.is_a?(eval(@reflection.class_name)) 
     213            raise ActiveRecord::AssociationTypeMismatch, "#{@reflection.class_name} (class id=#{@reflection.klass.object_id}) expected, got #{record.class} (class id=#{record.class.object_id})
    214214          end 
    215215        end 
    216216 
  • /dev/null

    old new  
     1require "cases/helper" 
     2require 'models/owner' 
     3require 'models/pet' 
     4 
     5class ReloadModelsTest < ActiveRecord::TestCase 
     6  def setup 
     7  end 
     8 
     9  def teardown 
     10  end 
     11 
     12  def test_has_one_with_reload 
     13    # Reload the class Owner, simulating auto-reloading of model classes in a 
     14    # development environment. Note that meanwhile the class Pet is not  
     15    # reloaded, simulating a class that is present in a plugin. 
     16    Object.class_eval { remove_const :Owner } 
     17    Kernel.load(File.expand_path(File.join(File.dirname(__FILE__), "../models/owner.rb"))) 
     18 
     19    pet = Pet.find_by_name('parrot') 
     20    pet.owner = Owner.find_by_name('ashley') 
     21    # After reload this assignment failed with the error message:  
     22    #   ActiveRecord::AssociationTypeMismatch: Owner expected, got Owner 
     23    # See also http://dev.rubyonrails.org/ticket/8246 
     24 
     25    assert_equal pet.owner, Owner.find_by_name('ashley') 
     26  end 
     27 
     28end