-
Notifications
You must be signed in to change notification settings - Fork 123
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
Add hash return type for relation groupchain size #2044
base: main
Are you sure you want to change the base?
Add hash return type for relation groupchain size #2044
Conversation
@@ -376,7 +376,7 @@ def create_group_chain_methods(klass) | |||
return_type: "T.self_type", | |||
) | |||
|
|||
CALCULATION_METHODS.each do |method_name| | |||
(CALCULATION_METHODS + [:size]).each do |method_name| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any reason to not add :size
into the CALCULATION_METHODS
array itself?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm I'm actually not sure how to categorize this. These arrays are mostly organized based on the class that they were defined in, except the methods defined on ActiveRecord::Relation
, where we have FIND_OR_CREATE_METHODS
, BUILDER_METHODS
, and TO_ARRAY_METHODS
. But :size
(like :to_a
) is not something you can call directly on the model's class, so we can't have it in CommonRelationMethods
or GeneratedRelationMethods
. Unlike :to_a
, :size
's return type is different depending on context, so it didn't seem to fit into any of the above arrays.
But now that you bring it up, this seems like one of those cases where the method name doesn't fit nicely into into an array and the method should be defined outside of the scope of the case statement looped over an array.
And when looking into how the .to_a
worked, I'm now finding that the GeneratedRelationMethods#where
chain typing is actually incorrect. If you give an argument like User.where(foo: 1)
you do not in fact get to use the methods on the PrivateAssociationRelationWhereChain
returned. Additionally, you can't call :to_a
on an object that is a where chain (User.where.not.to_a
), unlike what the generated rbi suggests.
I think the best thing to do here might be to take a step back and make sure this structure is actually correct, and to figure out a way to automatically test this so that this can't happen. This PR might be a symptom of a larger problem and could complicate things further, and the .to_a
additions were a reasonably good example of a case where this happened due to the assumption that the existing behavior was correct, when it wasn't. Should we close this for now or what do you think?
And now I can fully appreciate how difficult the job is of the Tapioca team!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey Mark!
where the method name doesn't fit nicely into into an array and the method should be defined outside of the scope of the case statement looped over an array.
That seems reasonable to me.
If you give an argument like User.where(foo: 1) you do not in fact get to use the methods on the PrivateAssociationRelationWhereChain returned.
I can't test this right now, could you explain the issue?
And now I can fully appreciate how difficult the job is of the Tapioca team!
Haha thanks! Trying to tame Ruby meta-programming with types is a formidable challenge :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd much rather see size
explicitly handled separately, since it is not a calculation method. Moreover, the signature generated for size
in this PR is not correct, since size
does not take any parameters like column_name
nor does it take a block.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Regarding this:
And when looking into how the .to_a worked, I'm now finding that the GeneratedRelationMethods#where chain typing is actually incorrect. If you give an argument like User.where(foo: 1) you do not in fact get to use the methods on the PrivateAssociationRelationWhereChain returned. Additionally, you can't call :to_a on an object that is a where chain (User.where.not.to_a), unlike what the generated rbi suggests.
Ufuk has opened a PR to make "where chains" no longer be relations: #2070
I'm OK with merging this as an incremental improvement over the current incorrect types. Could you please investigate the CI failures and see if they'r related to your changes? |
I've been extremely busy lately so I may not have time to get to it this week but will do what I can |
Motivation
Model.where(...).group(...).size
returns a hash, not a scalar number.Implementation
This is a special case where
ActiveRecord::Calculations
doesn't handle the size method.https://github.com/rails/rails/blob/6f57590388ca38ed2b83bc1207a8be13a9ba2aef/activerecord/lib/active_record/relation.rb#L273-L280
We can handle this by adding a special case for the size method as we can't rely on the Calculations class to provide the name of the method.
Tests
Yes