Skip to content

Commit

Permalink
Merge pull request #348 from masato-bkn/add-note-to-redundant-all-sec…
Browse files Browse the repository at this point in the history
…tion

Add note to redundant-all section about `all` receiver being an association
  • Loading branch information
koic authored Nov 15, 2023
2 parents ee8c5a1 + 7068195 commit 2d0a752
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Using `all` as a receiver is redundant. The result won't change without `all`, so it should be removed.

[source, ruby]
----
Expand All @@ -1121,6 +1120,17 @@ 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`:

* `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]

So, when considering removing `all` from the receiver of these methods, it is recommended to refer to the documentation to understand how the behavior changes.

== Migrations

=== Schema Version [[schema-version]]
Expand Down

0 comments on commit 2d0a752

Please sign in to comment.