Class: RuboCop::Cop::Rails::PluckInWhere
- Inherits:
-
RuboCop::Cop
- Object
- RuboCop::Cop
- RuboCop::Cop::Rails::PluckInWhere
- Includes:
- ActiveRecordHelper
- Defined in:
- lib/rubocop/cop/rails/pluck_in_where.rb
Overview
This cop identifies places where ‘pluck` is used in `where` query methods 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.
Constant Summary collapse
- MSG =
'Use `select` instead of `pluck` within `where` query method.'
Constants included from ActiveRecordHelper
ActiveRecordHelper::WHERE_METHODS
Instance Method Summary collapse
Methods included from ActiveRecordHelper
#external_dependency_checksum, #foreign_key_of, #in_where?, #resolve_relation_into_column, #schema, #table_name
Instance Method Details
#autocorrect(node) ⇒ Object
28 29 30 31 32 |
# File 'lib/rubocop/cop/rails/pluck_in_where.rb', line 28 def autocorrect(node) lambda do |corrector| corrector.replace(node.loc.selector, 'select') end end |
#on_send(node) ⇒ Object
24 25 26 |
# File 'lib/rubocop/cop/rails/pluck_in_where.rb', line 24 def on_send(node) add_offense(node, location: :selector) if node.method?(:pluck) && in_where?(node) end |