DEV Community

Cover image for Tips on Handling Hash Keys in Rails
Weseek-Inc
Weseek-Inc

Posted on • Edited on

Tips on Handling Hash Keys in Rails

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


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

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

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

2. Convert key from symbol to string

stringify_keys

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

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

convert hash nesting2

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

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

with_indifferent_access2

Try to get values in both strings and symbols.

with_indifferent_access3

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 and use in English.

For more information, go to GROWI.org to learn more about us. You can also follow our Facebook to see updates about our service.

GROWI.org

Top comments (0)