views:

49

answers:

2

Hello,

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-07-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.

A: 

I think it may be as straighforward as:

require 'yaml'
@article = YAML::load(@serialized_copy)
bjg
Thanks for answered. But this isn't, 'cause I would like to load though ActiveRecord (or ActiveModel) to the DB.
Puru puru rin..
A: 

Couldn't you just store the ID? This would allow you to stream the ID anywhere and not bother with the content.

François Beausoleil
No, I just would like to load a serialized object (in YAML or JSON) inside the DB. Is there a more generic way then: Article.create(ActiveSupport::JSON.decode(@article.to_json)[:article]), without specifying "article"?
Puru puru rin..
Are you looking for the serialize class-method? http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M001799
François Beausoleil