I am trying:
Product.first.attributes.map{|k, v| "#{k.to_sym} => #{v}"}
However, I receive the output as follows:
["id => 53", "name = blah"], ["id => 54", "name = blahblah"]
What I want is:
[{:id=>53,:name=>"blah"}, {:id=>54,:name=>"blahblah"}]
Looks like you’re just trying to convert the
Product.first.attributesHash to a Hash with symbols for keys rather than strings. You can make it easy on yourself by calling thesymbolize_keysthat Rails (ActiveSupport actually) has patched into Hash:When you say this:
you’re just producing a string that looks sort of like a Hash and that’s not terribly useful. If you want to symbolize the keys the long way, you’d probably produce an array of arrays using:
and then feed that whole thing to
Hash[]:I wouldn’t bother with all that noise though, just use
symbolize_keysand move on to more interesting problems.