Skip to content

Commit 74a8994

Browse files
committed
Add entries for pluck and pick
1 parent 85a902d commit 74a8994

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

README.adoc

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,38 @@ scope :chronological, -> { order(id: :asc) }
887887
scope :chronological, -> { order(created_at: :asc) }
888888
----
889889

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+
890922
=== `ids` [[ids]]
891923

892924
Favor the use of `ids` over `pluck(:id)`.

0 commit comments

Comments
 (0)