---
title: "Rails 6 adds ActiveRecord::Relation#pick"
description:
  "Rails 6 added a shortcut to select first value for a group of columns from a
  relation"
canonical_url: "https://www.bigbinary.com/blog/rails-6-adds-activerecord-relation-pick"
markdown_url: "https://www.bigbinary.com/blog/rails-6-adds-activerecord-relation-pick.md"
---

# Rails 6 adds ActiveRecord::Relation#pick

Rails 6 added a shortcut to select first value for a group of columns from a
relation

- Author: Prathamesh Sonpatki
- Published: January 16, 2019
- Categories: Rails 6

Before Rails 6, selecting only the first value for a column from a set of
records was cumbersome. Let's say we want only the first name from all the posts
with category "Rails 6".

```ruby
>> Post.where(category: "Rails 6").limit(1).pluck(:name).first
   SELECT "posts"."name"
   FROM "posts"
   WHERE "posts"."category" = ?
   LIMIT ?  [["category", "Rails 6"], ["LIMIT", 1]]
=> "Rails 6 introduces awesome shiny features!"
```

In Rails 6, the new
[ActiveRecord::Relation#pick](https://github.com/rails/rails/pull/31941) method
has been added which provides a shortcut to select the first value.

```ruby
>> Post.where(category: "Rails 6").pick(:name)
   SELECT "posts"."name"
   FROM "posts"
   WHERE "posts"."category" = ?
   LIMIT ?  [["category", "Rails 6"], ["LIMIT", 1]]
=> "Rails 6 introduces awesome shiny features!"
```

This method
[internally applies](https://github.com/rails/rails/blob/45b898afc07dca936df13795dd5179bff5ae9a90/activerecord/lib/active_record/relation/calculations.rb#L203-L219)
`limit(1)` on the relation before picking up the first value. So it is useful
when the relation is already reduced to a single row.

It can also select values for multiple columns.

```ruby
>> Post.where(category: "Rails 6").pick(:name, :author)
   SELECT "posts"."name", "posts"."author"
   FROM "posts"
   WHERE "posts"."category" = ?
   LIMIT ?  [["category", "Rails 6"], ["LIMIT", 1]]
=> ["Rails 6.0 new features", "prathamesh"]
```

## Links

- [Human page](https://www.bigbinary.com/blog/rails-6-adds-activerecord-relation-pick)
