ActiveRecords not serialized to yaml correctly

1.1k Views Asked by At

When I serialize active records I'm finding that the yaml format is different depending on which box I'm running on. On one box I get:

object: !ruby/object:User 

instead of

object: !ruby/ActiveRecord:User 

The first version is a problem because active support needs to do some magic to populate the active record correctly when the yaml is deserialized. What causes this difference and how can I ensure that the second format is used?


I'm using ruby 1.9.2 and I've forced the yaml engine to use syck using

require "yaml"
YAML::ENGINE.yamler = "syck"

in the boot.rb (rails app).

Update

Having dug a little further I've discovered that the ActiveRecord type isn't being registered with yaml. On the server where it works the following call:

YAML.resolver.tags.keys

includes:

"tag:ruby.yaml.org,2002:ActiveRecord"

This type is missing from the server that's behaving incorrectly. The problem now is that I have no idea why the type isn't registered.

2

There are 2 best solutions below

0
On BEST ANSWER

The problem turned out to be interactions between a few different environment issues that I didn't mention in the original question.

So the problem turned out to be a configuration issue with passenger. If you have a file in your project at config/setup_load_paths.rb then your Gemfile isn't loaded. I'd added

require "yaml"
YAML::ENGINE.yamler = "syck"

at the top of my Gemfile to ensure that the engine was selected before rails loaded and registered it's active record converter with yaml. Passenger wasn't running the Gemfile so the engine was never getting set and ruby was defaulting to pysch instead of syck. The current version of delayed_job forces the use of syck but because psych was being loaded the syck engine never had the active record converter registered with it.

0
On

This type of problem is usually caused by Rails class reloading. Did you try setting:

 config.cache_classes = true

in config/environments/development.rb?