Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserialization Error when loading ActiveRecord objects from YAML

Why do I get this error when I try to deserialize a new ActiveRecord object:

[Dev]> YAML.load(Identity.new.to_yaml)
Delayed::DeserializationError: Delayed::DeserializationError

while at the same time, this example with an existing AR object works as expected:

[Dev]> YAML.load(Identity.first.to_yaml)
=> #<Identity id: 1, ...

A few things that seem relevant, based on searching for answers:

I'm using ruby 1.9.2p318, rails 3.1.3, delayed_job 2.1.4 and my YAML::ENGINE.yamler is syck.

What can I do so that I can serialize a new Identity record as YAML and then deserialize it later?

UPDATE: I also discovered that if I remove the delayed_job gem then this simple example does work, and my YAML::ENGINE.yamler is now psych. But I use delayed_job in my app, so it's still important to understand what's going on

like image 940
tws Avatar asked Oct 23 '22 12:10

tws


1 Answers

Well the reason for that if you check the delayed_job structure there exist a file called serialization/active_record.rb

Now for Ruby with syck (only of syck it happen) as the YAML engine the define yaml_new is invoke everytime YAML.load is called and if you examine the method technically it try to build the object from the database with the id extracted from the attributes of the object

so for this

YAML.load(Identity.first.to_yaml)

it internally does this

Identity.find(1)

but in your case Identity.new as no id associate with it so in this is throws you of deserialization

I think the reason it took so long for people to respond to this question is that error listed by you

Delayed::DeserializationError: Delayed::DeserializationError

because it does state any information as to that there is no primary key present which it does currently

the reason I believe is this commit possible which is was pushed more or less at the same time when the you posted the question but perhaps you where still using the old code in your machine did not reported it this

ActiveRecord::RecordNotFound, class: #{klass} , primary key: #{val['attributes'][klass.primary_key]} 

NOTE :

It only happen for syck tested out pysch the method is not called at all perhaps it used for backward compatibility I guess

like image 176
Viren Avatar answered Oct 30 '22 17:10

Viren