을 직렬화 된 객체로 역 직렬화하려고합니다.데이터베이스에 대한 ActiveRecord 객체 직렬화 및 비 직렬화
>> 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">]
하지만 같은 어떤 것을 사용하여,이 조금 더 일반적인를 작성하려합니다 : 그래서는 (JSON 포함)로 처리하는 것이 가능
ActiveRecord.create(a)
보다는 :
Yea.create(a[:yea])
그렇게하는 방법을 알고 있습니까? 어떤 도움을 주셔서 감사합니다.
require 'yaml'
@article = YAML::load(@serialized_copy)
답변 해 주셔서 감사합니다. 하지만 이것은 ActiveRecord (또는 ActiveModel)를 DB에로드하려고하므로 발생하지 않습니다. –