Tips on Handling Hash Keys in Rails
Tags: rails, programming, ruby
Published: 2024-05-21 14:00:00

## Introduction

Hello everyone, this is Kota.
I use Rails in my daily work, and I would like to briefly share something I learned about hash keys that I handled the other day!

---

## Trouble
What I did was obtain the data from the model, put the variable into it, and hashed the variable using the attributes method.
However, when accessing the hashed variable with `user[:name]` to obtain the value of `name`, `nil` was outputed and the value could not be obtained.

![Trouble](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x9ejm2cb90cebl621fob.png)

---

## Investigation

As mentioned earlier, the return value when using the attributes method on the data obtained from the model was as follows. That's the time I noticed that **the key is a string**.

![Investigation1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8iky3p3tc1glzbs13qlk.png)

It is something obvious, but I realized again that the value can only be obtained by accessing it in the same format in which it is defined.

![Investigation2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/im5u1jzrg2xbyhb4fyso.png)

I was a little bit stuck from there because I often used symbols instead of strings to get the value of params, etc.

---
## Further Research & Methodology 

I took the opportunity to do a little more research on hash and compiled a list of methods that could be used when you are facing similar issues.

### 1. Convert key from string to symbol
- `symbolize_keys`
 - Methods provided by Rails. Internally, it uses ruby's **transform_keys(&:to_sym)** method.

- Reference: https://api.rubyonrails.org/classes/Hash.html#method-i-symbolize_keys

![symbolize_keys](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/miy7rxnwt060irj11s29.png)

### 2. Convert key from symbol to string
- `stringify_keys`
 - Methods provided by Rails. Internally, it uses ruby's **transform_keys(&:to_s)** method.
- Reference: https://api.rubyonrails.org/classes/Hash.html#method-i-stringify_keys

![stringify_keys](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/szszxyanq0x92duxmdub.png)

### 3. If you also want to convert hash-nesting
One caveat to the above two methods is that if there are nests within the hash, they will not be converted.

![convert hash nesting1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/441bt1akfy4xveinjlb0.png)

If you want to convert nests as well, you can use **`deep_symbolize_keys`** (`deep_stringify_keys`) to do so.

![convert hash nesting2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a0nprq9dx8m4piomxfbv.png)

_PS:The method `transform_keys` of ruby is also `deep_transform_keys`, which can transform nests._

### 4. If you want to get both symbol & string
- `with_indifferent_access`

Check the class of the variable `user` that was used earlier.

![with_indifferent_access1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tdso70the5ehx2x73uqm.png)

Now let's change the class to `ActiveSupport::HashWithIndifferentAccess` using the method `with_indifferent_access`.

- Reference:
https://api.rubyonrails.org/classes/ActiveSupport/HashWithIndifferentAccess.html

![with_indifferent_access2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6svglcp38o5edpkh14ni.png)

Try to get values in both strings and symbols.

![with_indifferent_access3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cow74khxw9zt8fuxhnl2.png)

Now you can retrieve the value as both a string and a symbol.
Params also extend this `ActiveSupport::HashWithIndifferentAccess` class and can retrieve values as both strings and symbols.

---

## Summary

In this article, I briefly summarized the handling of hash keys and related methods used in Rails.
There are many useful methods related to hash, so please check them out yourself.

---
## About Us💡
In addition, I want to introduce a little more about GROWI, an open software developed by us **WESEEK, Inc**.

**GROWI** is a wiki service with features-rich support for efficient information storage within the company. It also boasts high security and various authentication methods are available to simplify authentication management, including **LDAP/OAuth/SAML**.

**GROWI** originated in Japan and GROWI OSS is **FREE** for anyone to [download](https://docs.growi.org/en/admin-guide/?utm_source=dev+community&utm_medium=referral&utm_campaign=Tips_on_Handling_Hash_Keys_in_Rails) and use **in English**.

For more information, go to [GROWI.org](https://growi.org/en/?utm_source=dev+community&utm_medium=referral&utm_campaign=Tips_on_Handling_Hash_Keys_in_Rails) to learn more about us. You can also follow our [Facebook](https://www.facebook.com/people/GROWIcloud/100089272547238/) to see updates about our service.

![GROWI.org](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1rhpa0qcdukeall2vfsj.png)