If you have these classes:
class Foo < ActiveRecord::Base
has_many :things, as: :content
end
class Bar < Foo
end
class Thing < ActiveRecord::Base
belongs_to :content, polymorphic: true
end
Then assigning a Bar
to thing.content
then Rail sets thing.content_type
to 'Foo'
.
>> thing = Thing.new
>> bar = Bar.new
>> thing.content = bar
>> thing.content_type
=> "Foo"
Can you make Rails use the actual type of a polymorphic object as the type
key, rather than the type of the object's superclass?
@jcoglan, I remember seeing this but am drawing a blank on the solution. I feel like the answer is in here: https://github.com/rails/rails/blob/6ef0569b0bf6e13d63f6c51790745b2007b92973/activerecord/lib/active_record/inheritance.rb
Here's a test similar to yours: https://github.com/rails/rails/blob/0bccde963c0b3e2ad65b2bdac9d144f40854ec90/activerecord/test/cases/associations/belongs_to_associations_test.rb#L130 it looks like the behavior you're seeing is expected.