diff --git a/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb b/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb index 67e18d6..fba8fde 100644 --- a/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb +++ b/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb @@ -8,7 +8,7 @@ module ActiveRecord @target = (AssociationProxy === record ? record.target : record) @owner[@reflection.primary_key_name] = record_id(record) - @owner[@reflection.options[:foreign_type]] = record.class.base_class.name.to_s + @owner[@reflection.options[:foreign_type]] = record.class.name.to_s @updated = true end diff --git a/activerecord/test/cases/associations/belongs_to_associations_test.rb b/activerecord/test/cases/associations/belongs_to_associations_test.rb index 9f3945f..0a6e5d5 100644 --- a/activerecord/test/cases/associations/belongs_to_associations_test.rb +++ b/activerecord/test/cases/associations/belongs_to_associations_test.rb @@ -14,6 +14,7 @@ require 'models/tagging' require 'models/comment' require 'models/sponsor' require 'models/member' +require 'models/special_member' require 'models/essay' class BelongsToAssociationsTest < ActiveRecord::TestCase @@ -358,7 +359,21 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase sponsor.sponsorable = member assert_equal "Member", sponsor.sponsorable_type end + + def test_polymorphic_assignment_foreign_type_field_updating_with_single_table_inheritence + # should update when assigning a saved record + sponsor = Sponsor.new + member = SpecialMember.create + sponsor.sponsorable = member + assert_equal "SpecialMember", sponsor.sponsorable_type + # should update when assigning a new record + sponsor = Sponsor.new + member = SpecialMember.new + sponsor.sponsorable = member + assert_equal "SpecialMember", sponsor.sponsorable_type + end + def test_polymorphic_assignment_with_primary_key_foreign_type_field_updating # should update when assigning a saved record essay = Essay.new