Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Methods defined on PrivateRelation should return T.self_type #2124

Open
rzane opened this issue Dec 12, 2024 · 0 comments
Open

Methods defined on PrivateRelation should return T.self_type #2124

rzane opened this issue Dec 12, 2024 · 0 comments

Comments

@rzane
Copy link
Contributor

rzane commented Dec 12, 2024

I wrote a compiler for Kaminari that returns the following:

sig { T.all(PrivateRelation, Kaminari::PageScopeMethods, Kaminari::ActiveRecordRelationMethods) }
def page(num = nil); end

This type is accurate, since .page uses .extending to add methods to the relation.

This works:

User.page(5).total_count

This doesn't work:

User.page(5).all.total_count
                 ^^^^^^^^^^^ Method `total_count` does not exist on `User::PrivateRelation`

That's because the return type of for .all is PrivateRelation. We could fix this problem by using T.self_type instead, which I believe more accurately captures the way ActiveRecord works.

The way that relevant compilers are currently implemented makes this a little tricky, since the methods that are being added to the PrivateRelation are also added to the model. The methods defined on the model should be PrivateRelation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant