How does the polymorphic #{as}_type attribute get set in ActiveRecord models?

Hi people

I'm stumped. I've been playing with a little patch to the ActiveRecord inheritance, and it's broke a test, namely this one from associations/join_models_test.rb:

   def test_polymorphic_has_many_create_model_with_inheritance_and_custom_base_class
     post = SubStiPost.create :title => 'SubStiPost', :body => 'SubStiPost body'
     assert_instance_of SubStiPost, post

     tagging = tags(:misc).taggings.create(:taggable => post)
     assert_equal "SubStiPost", tagging.taggable_type
   end

It's failing with <"SubStiPost"> expected but was <"Post">.

I've deduced that "taggable_type" is stored as an attribute of the join model in has_many :through associations, but I can't work out how it is calculated or set. Is there an AR guru who can explain this bit to me?

Thanks
Ashley