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.
In ActiveRecord, chaining a manual .select (.select called with raw SQL) and .pluck produces unexpected results. Examples of this:
User.select('id AS id2').pluck(:id2) > Unknown column id2 (issue)
User.select('id, email AS user_email').pluck('id', 'user_email') > Unknown column user_email
Gift.select("DISTINCT(country_code)").pluck(:country_code) > Results are not de-duped (issue)
And other cases that work can be written more simply:
User.select('id').pluck(:id) can be written as User.select(:id).pluck(:id)
The nature of select, which loads up an ActiveRecord::Relation, is incongruous with pluck, which operates on the DB directly. At best the combination can work incidentally, and at worst it can produce silently unexpected results. As stated in the linked rails issue:
pluck selects only what was passed as its arguments (and ignores manual selects)
Describe the solution you'd like
Add a rule to disallow chaining manual selects with pluck. Provide helpful messaging that pluck will ignore the manual directive provided to select.
I am happy to contribute this rule if folks agree it's valuable!
Describe alternatives you've considered
Autocorrect .select("RAW").pluck to .select("RAW").to_a.pluck - technically works, but not always desired
Is your feature request related to a problem? Please describe.
In ActiveRecord, chaining a manual
.select
(.select
called with raw SQL) and.pluck
produces unexpected results. Examples of this:User.select('id AS id2').pluck(:id2)
> Unknown columnid2
(issue)User.select('id, email AS user_email').pluck('id', 'user_email')
> Unknown columnuser_email
Gift.select("DISTINCT(country_code)").pluck(:country_code)
> Results are not de-duped (issue)And other cases that work can be written more simply:
User.select('id').pluck(:id)
can be written asUser.select(:id).pluck(:id)
The nature of select, which loads up an
ActiveRecord::Relation
, is incongruous with pluck, which operates on the DB directly. At best the combination can work incidentally, and at worst it can produce silently unexpected results. As stated in the linked rails issue:Describe the solution you'd like
Add a rule to disallow chaining manual selects with pluck. Provide helpful messaging that
pluck
will ignore the manual directive provided toselect
.I am happy to contribute this rule if folks agree it's valuable!
Describe alternatives you've considered
.select("RAW").pluck
to.select("RAW").to_a.pluck
- technically works, but not always desired.select
with.pluck
- seems valuable, though perhaps overly broadThe text was updated successfully, but these errors were encountered: