ruby / ruby/gem_rbs_collection

`ActiveRecord::Associations::CollectionProxy#build` has incorrect block signature

Open
#48 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
303
Forks
136
Avg merge
5h 15m
Merged PRs (30d)
6

Description

When creating a new ActiveRecord model through an association, for example:

class Post < ActiveRecord::Base
  has_many comments
end

class Comment < ActiveRecord::Base
  belongs_to :post
end

post = Post.new
comment = post.comments.new

the current type definitions throw an error when validating in Steep due to an incorrect signature:

 [error] The method cannot be called without a block
│ Diagnostic ID: Ruby::RequiredBlockMissing
│
└     @comment = post.comments.new
                               ~~~

This specific one comes from ActiveRecord::Associations::CollectionProxy#new(build), which is defined on https://github.com/ruby/gem_rbs_collection/blob/main/gems/activerecord/6.0/activerecord-generated.rbs#L1156-L1158

      def build: (?::Hash[untyped, untyped] attributes) { () -> untyped } -> untyped

      alias new build

The patch to fix it is to make the block optional, ie ?{ () -> untyped }

There is another method definition under ActiveRecord::AssociationRelation that likely also needs to be updated: https://github.com/ruby/gem_rbs_collection/blob/main/gems/activerecord/6.0/activerecord-generated.rbs#L259-L261

I'm happy to make a PR to address, but not sure if you would prefer to update the generated files or the patch.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with gems/activerecord/6.0/activerecord-generated.rbs at the CollectionProxy#build definition and inspect the nearby ActiveRecord::AssociationRelation definition. Check whether the generated file or its patch is the project’s source of truth, then run the relevant Steep validation to confirm both association calls work without a block.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.