Kai
Kai

Reputation: 2205

How do I deserialize an Rails ActiveRecord serialized field on rails console?

In my database I have a table for an ActiveRecord type that has a serialized object (stored as "--- !ruby/hash:" followed by classname and key/values separated by newline). I have to reconstruct a part of the database manually and all I have now is a CSV dump of the rows. I am working on a solution to import the objects and restore entries, but I can't find a way to deserialize those serialized hashes. What Object/method is to call to restore these objects?

Upvotes: 1

Views: 1986

Answers (1)

user4776684
user4776684

Reputation:

It looks like you have a yaml object in your database. You can try the following:

YAML::load(your_column)

As an example conversion of a column with a Time object:

    [1] pry(main)> puts t.request
    --- 2016-06-22 01:23:54.038830166 +03:00
    ...
    => nil
    [2] pry(main)> YAML::load(t.request)
    => 2016-06-22 01:23:54 +0300
    [3] pry(main)> YAML::load(t.request).class
    => Time

Upvotes: 4

Related Questions