Skip to content

Commit 6c6b4fb

Browse files
committed
Mark Rails/Pluck as unsafe
1 parent 844055e commit 6c6b4fb

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

changelog/change_pluck_as_unsafe.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#826](https://github.com/rubocop/rubocop-rails/issues/826): Mark `Rails/Pluck` as unsafe. ([@fatkodima][])

config/default.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,9 @@ Rails/Pluck:
711711
Description: 'Prefer `pluck` over `map { ... }`.'
712712
StyleGuide: 'https://rails.rubystyle.guide#pluck'
713713
Enabled: 'pending'
714+
Safe: false
714715
VersionAdded: '2.7'
716+
VersionChanged: '<<next>>'
715717

716718
Rails/PluckId:
717719
Description: 'Use `ids` instead of `pluck(:id)` or `pluck(primary_key)`.'

lib/rubocop/cop/rails/pluck.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,22 @@ module Rails
99
# element in an enumerable. When called on an Active Record relation, it
1010
# results in a more efficient query that only selects the necessary key.
1111
#
12+
# @safety
13+
# This cop is unsafe because model can define attribute aliases.
14+
#
15+
# [source,ruby]
16+
# ----
17+
# class User < ApplicationRecord
18+
# alias_attribute :nickname, :name
19+
# end
20+
#
21+
# # Original code
22+
# User.map { |user| user[:nickname] } # => array of nicknames
23+
#
24+
# # After autocorrection
25+
# User.pluck(:nickname) # => raises ActiveRecord::StatementInvalid
26+
# ----
27+
#
1228
# @example
1329
# # bad
1430
# Post.published.map { |post| post[:title] }

0 commit comments

Comments
 (0)