Skip to content

Commit

Permalink
Clarify the wording of the Rails/PluckInWhere cop
Browse files Browse the repository at this point in the history
Clarifies the existing wording to be easier to understand and adds an additional
paragraph about possible performance implications of this cop.
  • Loading branch information
padarom committed Jul 5, 2024
1 parent 9e7ca20 commit a8cf910
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions lib/rubocop/cop/rails/pluck_in_where.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,26 @@ module Rails
# and can be replaced with `select`.
#
# Since `pluck` is an eager method and hits the database immediately,
# using `select` helps to avoid additional database queries.
# using `select` helps to avoid additional database queries by running as
# a subquery.
#
# This cop has two different enforcement modes. When the `EnforcedStyle`
# is `conservative` (the default) then only calls to `pluck` on a constant
# (i.e. a model class) in the `where` is used as offenses.
# This cop has two modes of enforcement. When the `EnforcedStyle` is set
# to `conservative` (the default), only calls to `pluck` on a constant
# (e.g. a model class) within `where` are considered offenses.
#
# @safety
# When the `EnforcedStyle` is `aggressive` then all calls to `pluck` in the
# `where` is used as offenses. This may lead to false positives
# as the cop cannot replace to `select` between calls to `pluck` on an
# `ActiveRecord::Relation` instance vs a call to `pluck` on an `Array` instance.
# When `EnforcedStyle` is set to `aggressive`, all calls to `pluck`
# within `where` are considered offenses. This might lead to false
# positives because the check cannot distinguish between calls to
# `pluck` on an `ActiveRecord::Relation` instance and calls to `pluck`
# on an `Array` instance.
#
# Additionally, when using a subquery with the SQL `IN` operator,
# databases like PostgreSQL and MySQL can't optimize complex queries as
# well. They need to scan all records of the outer table against the
# subquery result sequentially, rather than using an index. This can
# cause significant performance issues compared to writing the query
# differently or using `pluck`.
#
# @example
# # bad
Expand Down

0 comments on commit a8cf910

Please sign in to comment.