Dave
Dave

Reputation: 19120

How do I prevent string keys from being converted to symbols in Rails?

I'm using Rails 4.2. I noticed when I try and create an array of hashes, it seems my string keys are getting converted to symbols ...

> my_hash_arr = [{"name": "DA", "amount": 100000 }]
=> [{:name=>"DA", :amount=>100000}]

I don't want this. I want my key "name", to remain as a string and not be converted to a symbol key. How do I structure my array so this conversion doesn't take place.

Upvotes: 3

Views: 1241

Answers (1)

Vibol
Vibol

Reputation: 1168

If you want that, the correct syntax is changing : to => like this:

 my_hash_arr = [{"name" => "DA", "amount" => 100000 }]
=> [{"name"=>"DA", "amount"=>100000}]

Upvotes: 4

Related Questions