Skip to content

Commit b6e1d11

Browse files
authored
Merge pull request rubocop#924 from fatkodima/pluck-unsafe-autocorrection
Mark `Rails/Pluck` as unsafe autocorrection
2 parents a7e8494 + 6c6b4fb commit b6e1d11

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
@@ -687,7 +687,9 @@ Rails/Pluck:
687687
Description: 'Prefer `pluck` over `map { ... }`.'
688688
StyleGuide: 'https://rails.rubystyle.guide#pluck'
689689
Enabled: 'pending'
690+
Safe: false
690691
VersionAdded: '2.7'
692+
VersionChanged: '<<next>>'
691693

692694
Rails/PluckId:
693695
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)