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 ActiveRecord::Calculations module to ActiveRecord::Base #730

Conversation

tasuku-lab280
Copy link
Contributor

@tasuku-lab280 tasuku-lab280 commented Nov 21, 2024

I wanted to use ActiveRecord::Calculations methods like User.count as class methods of the model, so added code to extend ActiveRecord::Calculations module in ActiveRecord::Base.

@@ -17,6 +17,7 @@ module ActiveRecord
extend ::ActiveRecord::AttributeMethods::Write::ClassMethods
extend ::ActiveRecord::Attributes::ClassMethods
extend ::ActiveRecord::AutosaveAssociation::ClassMethods
extend ::ActiveRecord::Calculations
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tasuku-lab280 tasuku-lab280 marked this pull request as ready for review November 21, 2024 07:57
Copy link

@tasuku-lab280 Thanks for your contribution!

Please follow the instructions below for each change.
See also: https://github.com/ruby/gem_rbs_collection/blob/main/docs/CONTRIBUTING.md

Available commands

You can use the following commands by commenting on this PR.

  • /merge: Merge this PR if CI passes

activerecord

You changed RBS files for an existing gem.
You need to get approval from the reviewers of this gem.

@hibariya, @ksss, @Little-Rubyist, @tk0miya, please review this pull request.
If this change is acceptable, please make a review comment including APPROVE from here.
Screen Shot 2024-03-19 at 14 13 36

After that, the PR author or the reviewers can merge this PR.
Just comment /merge to merge this PR.

@tk0miya
Copy link
Contributor

tk0miya commented Nov 21, 2024

Thank you for your contribution.

Unfortunately, this is not correct. ActiveRecord::Base does not extend ActiveRecord::Calculations directly.
The reason why ActiveRecord::Base provides the .count method is ActiveRecord::Querying.
It delegates tons of methods to the .all method.

You can see the .count method in the list:
https://github.com/rails/rails/blob/v8.0.0/activerecord/lib/active_record/querying.rb#L19

Therefore, extending the Calculation module in this class is not good.

Instead, in activerecord.rbs, we list such methods up to ActiveRecord::Base::ClassMethods and _ActiveRecord_Relation_ClassMethods.

module ClassMethods[Model, Relation, PrimaryKey]

interface _ActiveRecord_Relation_ClassMethods[Model, Relation, PrimaryKey]

@tasuku-lab280
Copy link
Contributor Author

@tk0miya
Thanks for the review! I understand!
I will close this PR once I have implemented the type definitions based on the information you gave me, and I will remake the PR again once I have implemented the type definitions.

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

Successfully merging this pull request may close these issues.

2 participants