You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is your feature request related to a problem? Please describe.
Do not use .pluck on .select.
.select returns an ActiveRecord relation with only the selected column(s) marked for retrieval
.pluck returns an array of column values
Using them together is at best redundant and at worst confusing and inefficient.
When chained with .select, .pluck is unaware of any directive passed to .select
(e.g. column aliases or a DISTINCT clause). This can lead to unexpected behavior.
Example: simple select + pluck
# before
User.select(:id).pluck(:id)
# after
User.pluck(:id)
The .select is redundant. Use either .select or .pluck on its own.
Example: select with alias
# before
User.select('id, email AS user_email').pluck('id', 'user_email')
# after
User.pluck(:id, :email)
# after
User.select(:id, 'email AS user_email')
.pluck is unaware of the alias created by .select and will raise an "Unknown column" error.
If you need the alias, use .select on its own. Otherwise, consider using .pluck on its own.
Example: select with DISTINCT string
# before
User.select('DISTINCT email').pluck(:email)
# after
User.group(:email).pluck(:email)
# after
User.distinct.pluck(:email)
# after
User.distinct.select(:email)
.pluck is unaware of .select's DISTINCT directive and will load all User emails from the
database - including duplicates. Use either .select or .pluck on its own with .distinct,
or use .group (which can be more efficient).
Example: select with .distinct
# before
User.select(:company_id).distinct.pluck(:company_id)
# after
User.group(:company_id).pluck(:company_id)
# after
User.distinct.pluck(:company_id)
# after
User.distinct.select(:company_id)
The .select is redundant. Use either .select or .pluck on its own with .distinct,
or use .group (which can be more efficient).
Example: select with block
# before
User.select(&:active?).pluck(:id)
# after
User.where(active: true).pluck(:id)
# after (caution - potentially memory-intensive)
User.select(&:active?).map(&:id)
# after (caution - potentially memory-intensive)
User.filter(&:active?).map(&:id)
.select and .pluck make this statement look like an ActiveRecord operation, but under the hood
.select is loading all Users into memory before filtering them using the active? method in Ruby.
Use .where to avoid loading all Users into memory, or use .filter or .map to make it
clear to readers this is not an ActiveRecord operation.
Note: This one may be the most controversial case and could therefore optionally be covered.
Describe the solution you'd like
Add a rule to disallow using pluck on select. Optionally apply the rule to the block case.
Describe alternatives you've considered
Autocorrect .select("RAW").pluck to .select("RAW").to_a.pluck - technically works, but not always desired
Uh oh!
There was an error while loading. Please reload this page.
Is your feature request related to a problem? Please describe.
Do not use .pluck on .select.
Using them together is at best redundant and at worst confusing and inefficient.
When chained with .select, .pluck is unaware of any directive passed to .select
(e.g. column aliases or a DISTINCT clause). This can lead to unexpected behavior.
Example: simple select + pluck
Example: select with alias
Example: select with DISTINCT string
Example: select with .distinct
Example: select with block
Note: This one may be the most controversial case and could therefore optionally be covered.
Describe the solution you'd like
Add a rule to disallow using pluck on select. Optionally apply the rule to the block case.
Describe alternatives you've considered
.select("RAW").pluck
to.select("RAW").to_a.pluck
- technically works, but not always desiredThe text was updated successfully, but these errors were encountered: