Before Rails 6, loading only the first value from the set of records was not very straight forward.
Suppose we want to fetch the name of the
the first book published by author Behrouz A. Forouzan.
In order to load just single column value in the above case,
we need to add limit(1) and first along with pluck.
pick method
Rails 6 has addedActiveRecord::Relation#pick
to pick the first value
from the named column in the relation.
pick is useful when we have a relation
which is already narrowed down to a single row
as the method applies limit(1) on the relation internally.
As observed in the above query,
pick only loads the actual value,
not the entire record object
similar to pluck.
Also, the value is typecast by the column type.
pick method with multiple columns
This method can also be used to get multiple columns of the relation efficiently.
We can change the above query to include published_on in the result set.