---
title: "Rails 6 adds ActionDispatch::Request::Session#dig"
description: "Rails 6 adds ActionDispatch::Request::Session#dig"
canonical_url: "https://www.bigbinary.com/blog/rails-6-adds-actiondispatch-request-session-dig"
markdown_url: "https://www.bigbinary.com/blog/rails-6-adds-actiondispatch-request-session-dig.md"
---

# Rails 6 adds ActionDispatch::Request::Session#dig

Rails 6 adds ActionDispatch::Request::Session#dig

- Author: Amit Choudhary
- Published: September 18, 2019
- Categories: Rails 6, Rails

Rails 6 added
[ActionDispatch::Request::Session#dig](https://github.com/rails/rails/pull/32446).

This works the same way as Hash#dig (Link is not available).

It extracts the nested value specified by the sequence of keys.

Hash#dig (Link is not available) was introduced in `Ruby 2.3`.

Before Rails 6, we can achieve the same thing by first converting session to a
hash and then calling Hash#dig (Link is not available) on it.

Let's checkout how it works.

#### Rails 5.2

Let's add some user information in session and use dig after converting it to a
hash.

```ruby
>> session[:user] = { email: 'jon@bigbinary.com', name: { first: 'Jon', last: 'Snow' }  }

=> {:email=>"jon@bigbinary.com", :name=>{:first=>"Jon", :last=>"Snow"}}

>> session.to_hash

=> {"session_id"=>"5fe8cc73c822361e53e2b161dcd20e47", "_csrf_token"=>"gyFd5nEEkFvWTnl6XeVbJ7qehgL923hJt8PyHVCH/DA=", "return_to"=>"http://localhost:3000", "user"=>{:email=>"jon@bigbinary.com", :name=>{:first=>"Jon", :last=>"Snow"}}}


>> session.to_hash.dig("user", :name, :first)

=> "Jon"
```

#### Rails 6.0.0.rc1

Let's add the same information to session and now use `dig` on session object
without converting it to a hash.

```ruby
>> session[:user] = { email: 'jon@bigbinary.com', name: { first: 'Jon', last: 'Snow' }  }

=> {:email=>"jon@bigbinary.com", :name=>{:first=>"Jon", :last=>"Snow"}}

>> session.dig(:user, :name, :first)

=> "Jon"
```

Here is the relevant [pull request](https://github.com/rails/rails/pull/32446).

## Links

- [Human page](https://www.bigbinary.com/blog/rails-6-adds-actiondispatch-request-session-dig)
