Thursday, July 5, 2012

Convert string to serialize hash

Hello Rubies,
        Do you phase the problem ? - sometime  when we are retrieving the serialized hash field from table we might get string as response. So in that case you can use one of the below solutions.

Eg. 

Model - User.rb
class User < ActiveRecord::Base
   serialize :content, Hash
end

convert string to serialize hash format by 

# get the one of the record from users table
user = User.first

either using
    user.content.to_yaml
or
    YAML.parse(user.content)
or
    YAML.load(user.content)

No comments:

Post a Comment