Rails 6 introduces ActiveRecord::Relation#pick method
Rails 6 adds ActiveRecord::Relation pick method to select values of columns from the first record matching where conditions. This is a shorthand for plucking column names, limiting records and then finding first value for the matching criteria.
Let’s take an example.
Now, to find out the name of user with email
before Rails 6 we will get it done as given below.
Before Rails 6
After Rails 6
With addition of ActiveRecord::Relation#pick, the same result can be obtained with code given below.
As we can see, the result is same. Thus, we can use pick on ActiveRecord::Relation to select values of one or more columns of the first record matching where conditions.
In order to use ActiveRecord::Relation#pick
with Rails 4.2 or Rails 5 applications,
we can use the gem
Just add the gem to
And then you are ready to use
Subscribe to Ruby in Rails
Get the latest posts delivered right to your inbox