Search code examples
ruby-on-railssorbet

Tapioca not generating correct RBI file for Rails model


I'm migrating the way I generate RBI files from Sorbet to Tapioca. However, I am getting a recurring problem across a number of the RBI files for my Rails models. The RBI files seem to be tripping up when trying to produce a signature for a has_many relationship.

(Note - it's the RBI file itself rather than the model that is generating the warning)

In the user.rbi file, the signature for the user.plans has_many association is:

    sig { returns(::ActiveRecord::Associations::CollectionProxy[Plan]) }
    def plans; end

When I run srb tc I see the following error:

sorbet/rbi/dsl/user.rbi:110: Method [] does not exist on T.class_of(ActiveRecord::Associations::CollectionProxy) https://srb.help/7003
     110 |    sig { returns(::ActiveRecord::Associations::CollectionProxy[Plan]) }
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  Got T.class_of(ActiveRecord::Associations::CollectionProxy) originating from:
    sorbet/rbi/dsl/user.rbi:110:
     110 |    sig { returns(::ActiveRecord::Associations::CollectionProxy[Plan]) }
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  Did you mean:
    sorbet/rbi/gems/[email protected]:3238: ActiveRecord::Delegation#[]
    3238 |  def [](*args, &block); end

Solution

  • that is unfortunately, one of the rough-edges of Tapioca right now. Since the AR association code was extracted out of Shopify Core, it ended up inheriting that weird generic type for the CollectionProxy, without the associated RBI file to actually define the generic type.

    We are in the process of fixing this, as we speak, but in the meantime please take a look at this for a workaround: https://github.com/Shopify/tapioca/issues/179#issuecomment-738948320