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 note to redundant-all section about all receiver being an association #348

Merged
merged 1 commit into from
Nov 15, 2023

Conversation

masato-bkn
Copy link
Contributor

As pointed out #347, when the receiver of all is an association, there are methods whose behavior changes when all is omitted.

So, I've added a note to caution against omitting these methods.

README.adoc Outdated

The following methods behave differently without `all`:

* delete - https://api.rubyonrails.org/classes/ActiveRecord/Persistence/ClassMethods.html#method-i-delete[with all] / https://api.rubyonrails.org/classes/ActiveRecord/Associations/CollectionProxy.html#method-i-delete[without all]
Copy link
Contributor Author

@masato-bkn masato-bkn Oct 19, 2023

Choose a reason for hiding this comment

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

I'm torn about whether to add delete and destroy to this list for the following reasons:

  • These methods are not the targets for verification of cop.

  • The expected arguments differ depending on whether all is present or not. In the first place, it's not possible to omit all. Even if omitted, unlike delete_all, it seems unlikely that one wouldn't notice the change in behavior:thinking:

    user.articles.all.delete(id_or_array) # takes an id or an array as an argument
    # https://api.rubyonrails.org/classes/ActiveRecord/Persistence/ClassMethods.html#method-i-delete
    
    user.articles.delete(Article.first) # takes a records as an argument
    # https://api.rubyonrails.org/classes/ActiveRecord/Associations/CollectionProxy.html#method-i-delete
    
    user.articles.all.destroy(id) # takes an id as an argument
    # https://api.rubyonrails.org/classes/ActiveRecord/Persistence/ClassMethods.html#method-i-destroy
    
    user.articles.destroy(Article.first) # takes a records as an argument
    # https://api.rubyonrails.org/classes/ActiveRecord/Associations/CollectionProxy.html#method-i-destroy

@@ -1121,6 +1120,20 @@ users.where(id: ids)
user.articles.order(:created_at)
----

NOTE: When the receiver for `all` is an association, there are methods whose behavior changes by omitting `all`.

The following methods behave differently without `all`:
Copy link
Contributor Author

@masato-bkn masato-bkn Oct 19, 2023

Choose a reason for hiding this comment

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

I think it might be better to leave the specifics of the differences in behavior to the official Rails documentation, rather than detailing them in this style guide. So, I only included the URL to the documentation and did not write about how each method behaves differently in the style guide.

@masato-bkn masato-bkn force-pushed the add-note-to-redundant-all-section branch from 96216bb to 0e9f9d0 Compare October 19, 2023 09:01
@masato-bkn masato-bkn marked this pull request as ready for review October 19, 2023 09:09
README.adoc Outdated
@@ -1103,8 +1103,7 @@ User.where.not('status = ? AND plan = ?', 'active', 'basic')

=== Redundant `all` [[redundant-all]]

Using `all` as a receiver for Active Record query methods is redundant.
The result won't change without `all`, so it can be removed.
When the receiver for Active Record query methods is `all`, the result won't change if `all` is omitted.
Copy link
Member

Choose a reason for hiding this comment

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

Can we consider ‘delete*’ “query methods”?
If not, we should emphasize on that.

README.adoc Outdated
@@ -1103,8 +1103,7 @@ User.where.not('status = ? AND plan = ?', 'active', 'basic')

=== Redundant `all` [[redundant-all]]

Using `all` as a receiver for Active Record query methods is redundant.
The result won't change without `all`, so it can be removed.
When the receiver for Active Record query methods is `all`, the result won't change if `all` is omitted.
Copy link
Member

Choose a reason for hiding this comment

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

Can we consider ‘delete*’ “query methods”?
The cop goes beyond just query methods.
What if we briefly mention that ‘all’ is redundant except for those not getting i to details?

Copy link
Contributor Author

@masato-bkn masato-bkn Oct 22, 2023

Choose a reason for hiding this comment

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

Thank you for your comment!

It's a tough call, but I consider delete* as query methods. Upon checking the ActiveRecord::Querying::QUERYING_METHODS constant in ActiveRecord, it includes methods like delete* and update*. Given this, in the context of ActiveRecord, it could be argued that delete* is also treated as a query method.

However, for those unfamiliar with these specifics, we might consider revising the phrasing.

What if we briefly mention that ‘all’ is redundant except for those not getting into details?

What specific wording do you think would be appropriate? I'd appreciate any suggestions you might have.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I made revisions based on the feedback. How does this look? 5efdfa8

Copy link
Member

@pirj pirj left a comment

Choose a reason for hiding this comment

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

Thanks!

README.adoc Outdated Show resolved Hide resolved
README.adoc Outdated
Comment on lines 1127 to 1133
* delete - https://api.rubyonrails.org/classes/ActiveRecord/Persistence/ClassMethods.html#method-i-delete[with all] / https://api.rubyonrails.org/classes/ActiveRecord/Associations/CollectionProxy.html#method-i-delete[without all]

* delete_all - https://api.rubyonrails.org/classes/ActiveRecord/Relation.html#method-i-delete_all[with all] / https://api.rubyonrails.org/classes/ActiveRecord/Associations/CollectionProxy.html#method-i-delete_all[without all]

* destroy - https://api.rubyonrails.org/classes/ActiveRecord/Persistence/ClassMethods.html#method-i-destroy[with all] / https://api.rubyonrails.org/classes/ActiveRecord/Associations/CollectionProxy.html#method-i-destroy[without all]

* destroy_all - https://api.rubyonrails.org/classes/ActiveRecord/Relation.html#method-i-destroy_all[with all] / https://api.rubyonrails.org/classes/ActiveRecord/Associations/CollectionProxy.html#method-i-destroy_all[without all]
Copy link
Member

Choose a reason for hiding this comment

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

Please remove the redundant blank lines in the list items and surround the method names with backticks.

Suggested change
* delete - https://api.rubyonrails.org/classes/ActiveRecord/Persistence/ClassMethods.html#method-i-delete[with all] / https://api.rubyonrails.org/classes/ActiveRecord/Associations/CollectionProxy.html#method-i-delete[without all]
* delete_all - https://api.rubyonrails.org/classes/ActiveRecord/Relation.html#method-i-delete_all[with all] / https://api.rubyonrails.org/classes/ActiveRecord/Associations/CollectionProxy.html#method-i-delete_all[without all]
* destroy - https://api.rubyonrails.org/classes/ActiveRecord/Persistence/ClassMethods.html#method-i-destroy[with all] / https://api.rubyonrails.org/classes/ActiveRecord/Associations/CollectionProxy.html#method-i-destroy[without all]
* destroy_all - https://api.rubyonrails.org/classes/ActiveRecord/Relation.html#method-i-destroy_all[with all] / https://api.rubyonrails.org/classes/ActiveRecord/Associations/CollectionProxy.html#method-i-destroy_all[without all]
* `delete` - https://api.rubyonrails.org/classes/ActiveRecord/Persistence/ClassMethods.html#method-i-delete[with all] / https://api.rubyonrails.org/classes/ActiveRecord/Associations/CollectionProxy.html#method-i-delete[without all]
* `delete_all` - https://api.rubyonrails.org/classes/ActiveRecord/Relation.html#method-i-delete_all[with all] / https://api.rubyonrails.org/classes/ActiveRecord/Associations/CollectionProxy.html#method-i-delete_all[without all]
* `destroy` - https://api.rubyonrails.org/classes/ActiveRecord/Persistence/ClassMethods.html#method-i-destroy[with all] / https://api.rubyonrails.org/classes/ActiveRecord/Associations/CollectionProxy.html#method-i-destroy[without all]
* `destroy_all` - https://api.rubyonrails.org/classes/ActiveRecord/Relation.html#method-i-destroy_all[with all] / https://api.rubyonrails.org/classes/ActiveRecord/Associations/CollectionProxy.html#method-i-destroy_all[without all]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've addressed it with d41903b.

@masato-bkn masato-bkn force-pushed the add-note-to-redundant-all-section branch 2 times, most recently from a155abb to d41903b Compare November 15, 2023 05:36
@koic
Copy link
Member

koic commented Nov 15, 2023

This looks good. Can you squash your commits into one?

FYI, in reality, as a reviewer wanting to see the differences, it's sufficient to check using "Compare" link, so there's no need to commits as a work log :-)

@masato-bkn masato-bkn force-pushed the add-note-to-redundant-all-section branch from d41903b to 7068195 Compare November 15, 2023 05:53
@masato-bkn
Copy link
Contributor Author

masato-bkn commented Nov 15, 2023

Thank you all for your reviews! I have squashed the commits, so please check it.

@koic koic merged commit 2d0a752 into rubocop:master Nov 15, 2023
3 checks passed
@koic
Copy link
Member

koic commented Nov 15, 2023

Thanks!

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

Successfully merging this pull request may close these issues.

3 participants