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

Add hash return type for relation groupchain size #2044

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

marknuzz
Copy link
Contributor

@marknuzz marknuzz commented Oct 15, 2024

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

# activerecord-7.1.4/lib/active_record/relation.rb
def size
  if loaded?
    records.length
  else
    count(:all)
  end
end
Model.group('created_at').count(:all) == Model.group('created_at').size
=> true

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

@marknuzz marknuzz requested a review from a team as a code owner October 15, 2024 03:55
@amomchilov amomchilov added the enhancement New feature or request label Oct 21, 2024
@@ -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|
Copy link
Contributor

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?

Copy link
Contributor Author

@marknuzz marknuzz Oct 22, 2024

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!

Copy link
Contributor

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 :)

Copy link
Member

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.

Copy link
Contributor

@amomchilov amomchilov Nov 9, 2024

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

@amomchilov
Copy link
Contributor

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?

@marknuzz
Copy link
Contributor Author

marknuzz commented Nov 5, 2024

I've been extremely busy lately so I may not have time to get to it this week but will do what I can

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

Successfully merging this pull request may close these issues.

3 participants