Repository navigation
Add types for methods ActiveRecord::Relation delegates to records - #1112
Draft
akitoshiga wants to merge 1 commit into
Draft
akitoshiga wants to merge 1 commit into
akitoshiga wants to merge 1 commit into
Conversation
ActiveRecord::Relation delegates Array methods such as length, [], join and sample to records. The generated RBS defines them in ActiveRecord::Delegation as untyped, so the element type of the relation is lost and callers have to insert to_a to get typed values. Define them in an interface and include it into ActiveRecord::Relation::Methods so that they are typed with the model of each relation. Co-authored-by: Cursor <[email protected]>
akitoshiga
force-pushed
the
activerecord-relation-delegate-to-records
branch
from
October 22, 2026 08:29
dff6d9a to
5c03ec9
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
ActiveRecord::Relationdelegates some Array methods torecords:https://github.com/rails/rails/blob/v8.1.3.1/activerecord/lib/active_record/relation/delegation.rb#L100-L103
In the generated RBS, these methods are defined in
ActiveRecord::Delegationas(*untyped, **untyped) ?{ (*untyped) -> untyped } -> untyped, so the element type of a relation is lost (e.g.User.all[0]isuntyped).This PR adds
ActiveRecord::Delegation::_DelegateToRecords[Model]and includes it intoActiveRecord::Relation::Methods[Model, PrimaryKey], so they are typed with the model of each relation.The signatures follow
Arrayin core RBS andArrayextensions in activesupport.eachis excluded becauseRelation::Methodsalready defines it.Why the signatures are written one by one
RBS has no syntax for delegation, and there is no way to refer to the method types of another class or to pick only some methods of
Array[Model]. So reusingArray's signatures would mean taking all of them, which does not match the actual behavior:Array[Model]:Relationis not anArray, and its methods would conflict with query methods such asselectandfind.Array: methodsRelationdoes not have (e.g.push,map!) would also type-check.The interface takes the model as a type parameter, so this single definition applies to the
ActiveRecord_Relationof every model generated by rbs_rails.Notes
ActiveRecord::Associations::CollectionProxydefines some of these methods (e.g.length) by itself, and those definitions take precedence over the included interface. Souser.articles.lengthis stilluntyped, while methods not defined inCollectionProxy(e.g.user.articles[0]) are typed.