ActiveRecord object serialization and deserialization to the database
I would like to deserialize an serialized object. So it's possible to process such as (with JSON):
>> l = Yea.create(:title => "foo bar")
=> #<Yea id: 3, title: "foo bar", created_at: "2010-07-05 21:44:54", updated_at: "2010-07-05 21:44:54">
>> j = l.to_json
=> "{\"yea\":{\"created_at\":\"2010-07-05T21:44:54Z\",\"title\":\"foo bar\",\"updated_at\":\"2010-07-05T21:44:54Z\",\"id\":3}}"
>> Yea.delete(3)
=> 1
>> a = ActiveSupport::JSON.decode(j)
=> {"yea"=>{"created_at"=>"2010-0开发者_如何转开发7-05T21:44:54Z", "title"=>"foo bar", "updated_at"=>"2010-07-05T21:44:54Z", "id"=>3}}
>> Yea.create(a[:yea])
=> [#<Yea id: 4, title: "foo bar", created_at: "2010-07-05 21:44:54", updated_at: "2010-07-05 21:44:54">]
But I would like to write this a little bit more generic, using some thing like:
ActiveRecord.create(a)
rather than:
Yea.create(a[:yea])
Do you know how to do so? Thank you for any help.
I think it may be as straighforward as:
require 'yaml'
@article = YAML::load(@serialized_copy)
Couldn't you just store the ID? This would allow you to stream the ID anywhere and not bother with the content.
精彩评论