class RuboCop::Cop::Rails::PluckInWhere
Post.where(user_id: active_users.pluck(:id))
# bad
@example EnforcedStyle: aggressive
Post.where(user_id: active_users.pluck(:id))
# good
@example EnforcedStyle: conservative (default)
Post.where(user_id: active_users.select(:id))
Post.where(user_id: User.active.select(:id))
# good
Post.where(user_id: User.active.pluck(:id))
# bad
@example
`ActiveRecord::Relation` instance vs a call to ‘pluck` on an `Array` instance.
as the cop cannot replace to `select` between calls to `pluck` on an
`where` is used as offenses. This may lead to false positives
When the `EnforcedStyle` is `aggressive` then all calls to `pluck` in the
@safety
(i.e. a model class) in the `where` is used as offenses.
is `conservative` (the default) then only calls to `pluck` on a constant
This cop has two different enforcement modes. When the `EnforcedStyle`
using `select` helps to avoid additional database queries.
Since `pluck` is an eager method and hits the database immediately,
and can be replaced with `select`.
Identifies places where `pluck` is used in `where` query methods
def on_send(node)
def on_send(node) return unless in_where?(node) return if style == :conservative && !root_receiver(node)&.const_type? range = node.loc.selector add_offense(range) do |corrector| corrector.replace(range, 'select') end end
def root_receiver(node)
def root_receiver(node) receiver = node.receiver if receiver&.send_type? root_receiver(receiver) else receiver end end