-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Add note to redundant-all section about all
receiver being an association
#348
Conversation
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] |
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'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 omitall
. Even if omitted, unlikedelete_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`: |
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 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.
96216bb
to
0e9f9d0
Compare
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. |
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.
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. |
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.
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?
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.
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.
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 made revisions based on the feedback. How does this look? 5efdfa8
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.
Thanks!
README.adoc
Outdated
* 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] |
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.
Please remove the redundant blank lines in the list items and surround the method names with backticks.
* 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] |
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've addressed it with d41903b.
a155abb
to
d41903b
Compare
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 :-) |
d41903b
to
7068195
Compare
Thank you all for your reviews! I have squashed the commits, so please check it. |
Thanks! |
As pointed out #347, when the receiver of
all
is an association, there are methods whose behavior changes whenall
is omitted.So, I've added a note to caution against omitting these methods.