I use hstore with Postgres 9.2 and Rails 3.2 to store my object like this:
class User
user_hstore = {:user_id =>"123", :user_courses => [1,2,3]}
end
Now, when I retrieve user_courses, I get a string like this:
'[1, 2, 3]'
How do I convert this string to Rails array? Better yet, is there a way to store an array within a hstore object so that Rails will automatically retrieve it as array type?
JSON.parse "[\"1018\", \"1037\", \"1045\", \"1042\"]"
#=> ["1018", " 1037", " 1045", " 1042"]
Why not just use eval
?
eval('[1, 2, 3]')
#=> [1, 2, 3]
Obviously, don't do this on arbitrary or user inputted data, but on an array of integers as you've displayed, it's perfectly safe.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With