File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change @@ -887,6 +887,38 @@ scope :chronological, -> { order(id: :asc) }
887
887
scope :chronological, -> { order(created_at: :asc) }
888
888
----
889
889
890
+ === `pluck`
891
+
892
+ Use https://api.rubyonrails.org/classes/ActiveRecord/Calculations.html#method-i-pluck[pluck] to select a single value from multiple records.
893
+
894
+ [source,ruby]
895
+ ----
896
+ # bad
897
+ User.all.map(&:name)
898
+
899
+ # bad
900
+ User.all.map { |user| user[:name] }
901
+
902
+ # good
903
+ User.pluck(:name)
904
+ ----
905
+
906
+ === `pick`
907
+
908
+ Use https://api.rubyonrails.org/classes/ActiveRecord/Calculations.html#method-i-pick[pick] to select a single value from a single record.
909
+
910
+ [source,ruby]
911
+ ----
912
+ # bad
913
+ User.pluck(:name).first
914
+
915
+ # bad
916
+ User.first.name
917
+
918
+ # good
919
+ User.pick(:name)
920
+ ----
921
+
890
922
=== `ids` [[ids]]
891
923
892
924
Favor the use of `ids` over `pluck(:id)`.
You can’t perform that action at this time.
0 commit comments