tapioca icon indicating copy to clipboard operation
tapioca copied to clipboard

Feature request: Support types for polymorphic associations

Open danny-pflughoeft opened this issue 7 months ago • 0 comments

Take the follow code, which defines a myParent.child relation:

class MyParent < Versions::VersionedApplicationRecord
  VALID_CHILD_TYPES = T.let([MyChild1.polymorphic_name, MyChild2.polymorphic_name].freeze, T::Array[String])

  has_one :child, polymorphic: true
  validates :child_type, presence: true, inclusion: { in: VALID_CHILD_TYPES }
end

In theory, there should be enough information to statically type the child association as T.any(MyChild1, MyChild2). However, what it currently gets typed as is T.untyped. It would be great if this common use-case were supported.

(Note that MyClass1.polymorphic_name is not necessarily the same as MyClass1.name. I believe you can call polymorphic_class_for to map the polymorphic_name to the class instance)

danny-pflughoeft avatar May 12 '25 20:05 danny-pflughoeft