---
title: "Rails 6 adds support of symbol keys"
description:
  "Rails 6 adds support of symbol keys with
  ActiveSupport::HashWithIndifferentAccess#assoc"
canonical_url: "https://www.bigbinary.com/blog/rails-6-adds-activesupport-hashwithindifferentaccess-assoc"
markdown_url: "https://www.bigbinary.com/blog/rails-6-adds-activesupport-hashwithindifferentaccess-assoc.md"
---

# Rails 6 adds support of symbol keys

Rails 6 adds support of symbol keys with
ActiveSupport::HashWithIndifferentAccess#assoc

- Author: Amit Choudhary
- Published: August 20, 2019
- Categories: Rails 6, Rails

Rails 6 added support of symbol keys with
[ActiveSupport::HashWithIndifferentAccess#assoc](https://api.rubyonrails.org/v5.2/classes/ActiveSupport/HashWithIndifferentAccess.html#method-i-assoc).

Please note that documentation of
[ActiveSupport::HashWithIndifferentAccess#assoc](https://api.rubyonrails.org/v5.2/classes/ActiveSupport/HashWithIndifferentAccess.html#method-i-assoc)
in Rails 5.2 shows that
[ActiveSupport::HashWithIndifferentAccess#assoc](https://api.rubyonrails.org/v5.2/classes/ActiveSupport/HashWithIndifferentAccess.html#method-i-assoc)
works with symbol keys but it doesn't.

In Rails 6,
[ActiveSupport::HashWithIndifferentAccess](https://github.com/rails/rails/pull/35080)
implements a hash where string and symbol keys are considered to be the same.

Before Rails 6, `HashWithIndifferentAccess#assoc` used to work with just string
keys.

Let's checkout how it works.

#### Rails 5.2

Let's create an object of
[ActiveSupport::HashWithIndifferentAccess](https://api.rubyonrails.org/v5.2/classes/ActiveSupport/HashWithIndifferentAccess.html)
and call `assoc` on that object.

```ruby
>> info = { name: 'Mark', email: 'mark@bigbinary.com' }.with_indifferent_access

=> {"name"=>"Mark", "email"=>"mark@bigbinary.com"}

>> info.assoc(:name)

=> nil

>> info.assoc('name')

=> ["name", "Mark"]
```

We can see that `assoc` does not work with symbol keys with
[ActiveSupport::HashWithIndifferentAccess](https://api.rubyonrails.org/v5.2/classes/ActiveSupport/HashWithIndifferentAccess.html)
in Rails 5.2.

#### Rails 6.0.0.beta2

Now, let's call `assoc` on the same hash in Rails 6 with both string and symbol
keys.

```ruby
>> info = { name: 'Mark', email: 'mark@bigbinary.com' }.with_indifferent_access

=> {"name"=>"Mark", "email"=>"mark@bigbinary.com"}

>> info.assoc(:name)

=> ["name", "Mark"]

>> info.assoc('name')

=> ["name", "Mark"]
```

As we can see, `assoc` works perfectly fine with both string and symbol keys
with
[ActiveSupport::HashWithIndifferentAccess](https://api.rubyonrails.org/v5.2/classes/ActiveSupport/HashWithIndifferentAccess.html)
in Rails 6.

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

## Links

- [Human page](https://www.bigbinary.com/blog/rails-6-adds-activesupport-hashwithindifferentaccess-assoc)
