Skip to content

Commit 3e91c03

Browse files
authored
Merge pull request #1307 from padarom/fix-pluck-in-where-documentation
Clarify the wording of the `Rails/PluckInWhere` cop
2 parents 6d55fb5 + a8cf910 commit 3e91c03

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

lib/rubocop/cop/rails/pluck_in_where.rb

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,26 @@ module Rails
77
# and can be replaced with `select`.
88
#
99
# Since `pluck` is an eager method and hits the database immediately,
10-
# using `select` helps to avoid additional database queries.
10+
# using `select` helps to avoid additional database queries by running as
11+
# a subquery.
1112
#
12-
# This cop has two different enforcement modes. When the `EnforcedStyle`
13-
# is `conservative` (the default) then only calls to `pluck` on a constant
14-
# (i.e. a model class) in the `where` is used as offenses.
13+
# This cop has two modes of enforcement. When the `EnforcedStyle` is set
14+
# to `conservative` (the default), only calls to `pluck` on a constant
15+
# (e.g. a model class) within `where` are considered offenses.
1516
#
1617
# @safety
17-
# When the `EnforcedStyle` is `aggressive` then all calls to `pluck` in the
18-
# `where` is used as offenses. This may lead to false positives
19-
# as the cop cannot replace to `select` between calls to `pluck` on an
20-
# `ActiveRecord::Relation` instance vs a call to `pluck` on an `Array` instance.
18+
# When `EnforcedStyle` is set to `aggressive`, all calls to `pluck`
19+
# within `where` are considered offenses. This might lead to false
20+
# positives because the check cannot distinguish between calls to
21+
# `pluck` on an `ActiveRecord::Relation` instance and calls to `pluck`
22+
# on an `Array` instance.
23+
#
24+
# Additionally, when using a subquery with the SQL `IN` operator,
25+
# databases like PostgreSQL and MySQL can't optimize complex queries as
26+
# well. They need to scan all records of the outer table against the
27+
# subquery result sequentially, rather than using an index. This can
28+
# cause significant performance issues compared to writing the query
29+
# differently or using `pluck`.
2130
#
2231
# @example
2332
# # bad

0 commit comments

Comments
 (0)