The question is: how can we retrieve a owners class of a polymorphic association without loading the owner object into memory?
class Source < ApplicationRecord self.has_one :state, dependent: :delete, as: :stateable end class State < ApplicationRecord belongs_to :stateable, polymorphic: true end
Solution
There are (at least) two ways to do this. First one is not recommended (as it might fail with multiple namespaces with same class names, etc:
State.last.stateable_type.constantize #=> Source
or a better one that will work out of any scope:
State.last.association(:stateable).klass #=> Source