How can I serialize - deserialize a hash in order to save it in the database?

Viewed 11125

I have a hash:

h = {
    "revision"=>7,         
    "rev"=>"708a4bd5b", 
    "thumb_exists"=>false, 
    "bytes"=>246000,     
    "modified"=>"Sun, 01 Jul 2012 17:09:15 +0000", 
    "client_mtime"=>"Sun, 01 Jul 2012 17:09:15     +0000", 
    "path"=>"/Getting Started.pdf", 
    "is_dir"=>false,       
    "icon"=>"page_white_acrobat", 
    "root"=>"dropbox",     
    "mime_type"=>"application/pdf", 
    "size"=>"240.2 KB"
}

I would like to save it in a database with the following command: h.to_s Then I would like to get the content from the database and to work with it as hash.

s = MyModel[:field_which_contains_hash_string]

I tried to load the content with YAML::load s but I get an error:

Psych::SyntaxError: (<unknown>): found unexpected ':' while scanning a plain scalar at line 1 column 96

I guess that's due to the colon in the time string. So what's the best way to persist the hash and retrieve it again?

Help is appreciated. Best, Philip

1 Answers
Related