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
If column name is aliased in a select clause, using .pluck('alias_column') in place of .map { |post| post['alias_column'] } will cause an ActiveRecord::StatementInvalid
Autocorrect should not result in changes that raise an error
Actual behavior
Autocorrect causes ActiveRecord::StatementInvalid
Steps to reproduce the problem
Add a #join and #select clause to an ActiveRecord::Relation. (e.g. .select("table_1.*", "table_2.id AS table_2_id"))
Using .map { |x| x['table_2_id'] } returns an Array of the aliased column
Using .pluck('table_2_id') raises an ActiveRecord::StatementInvalid because table_2_id is not a table column
Using .to_a.pluck('table_2_id') returns an Array of the aliased column
+1 unfortunately, the cop is inherently unsafe when column aliasing is used:
User.select('id AS id2').map{ |user| user[:id2]}# works ✔️User.select('id AS id2').pluck(:id2)# ERROR: column "id2" does not exist ❌User.select('id AS id2').to_a.pluck(:id2)# works ✔️
If column name is aliased in a select clause, using
.pluck('alias_column')
in place of.map { |post| post['alias_column'] }
will cause anActiveRecord::StatementInvalid
Cop docs:
Expected behavior
Autocorrect should not result in changes that raise an error
Actual behavior
Autocorrect causes ActiveRecord::StatementInvalid
Steps to reproduce the problem
#join
and#select
clause to anActiveRecord::Relation
. (e.g..select("table_1.*", "table_2.id AS table_2_id")
)Using
.map { |x| x['table_2_id'] }
returns anArray
of the aliased columnUsing
.pluck('table_2_id')
raises an ActiveRecord::StatementInvalid becausetable_2_id
is not a table columnUsing
.to_a.pluck('table_2_id')
returns anArray
of the aliased columnRuboCop version
The text was updated successfully, but these errors were encountered: