Serializing objects to database

I’m trying to serialize, err Marshal, an AR model with associations
to a db field.

Seems simple enough to start with. After a dump I have a bunch of
Binary data in my field, and looking at with a Hex viewer, it seems
to have AR-looking stuff.

However, when I try to re-constitute the model, I get an error
“undefined class/module Xyz” where Xyz is one of the associated models.

This is in Dev mode, so, figuring this just an issue of Rails not
having loaded that model file yet, I just tossed a require for each
of the associated models at the top of the controller file doing the
Marshal.load to ensure they are loaded first.

It’s simple, and it worked, but I’m wondering if there is another
solution, or, “yep, that’s how we fix that”

– gw

Greg W. wrote:

I’m trying to serialize, err Marshal, an AR model with associations
to a db field.

Seems simple enough to start with. After a dump I have a bunch of
Binary data in my field, and looking at with a Hex viewer, it seems
to have AR-looking stuff.

However, when I try to re-constitute the model, I get an error
“undefined class/module Xyz” where Xyz is one of the associated models.

This is in Dev mode, so, figuring this just an issue of Rails not
having loaded that model file yet, I just tossed a require for each
of the associated models at the top of the controller file doing the
Marshal.load to ensure they are loaded first.

It’s simple, and it worked, but I’m wondering if there is another
solution, or, “yep, that’s how we fix that”

– gw

I haven’t peered into how Marshal works itself.

But I imagine the problem is that it bypasses const_missing,
so rails’ clever file_name const-get-ing doesnt work.

d = RudeQueue.get(:rar)
=>
“\004\bo:\nEvent\t:\026@attributes_cache{\000:\020@attributes{\f”\rend_date0"\017start_date0"\tname"\tblah"\017updated_atu:\tTime\r�\e\200\273\0259R"\aidi\006"\020description0"\017created_at@\r:\f@errorso:\031ActiveRecord::Errors\a:\n@base@\000;\t{\000:\020@new_recordF"

Marshal.load(d)
ArgumentError: undefined class/module Event
from (irb):3:in `load’
from (irb):3

Event
=> Event(id: integer, name: string, description: text, start_date: date,
end_date: date, created_at: datetime, updated_at: datetime)

Marshal.load(d)
=> #<Event id: 1, name: “blah”, description: nil, start_date: nil,
end_date: nil, created_at: “2008-06-22 15:20:35”, updated_at:
“2008-06-22 15:20:35”>

So I imagine there is no clever way to fix it,
except to ensure that the related class is already loaded.

On 22 Jun 2008, at 15:27, Matthew R. Jacobs wrote:

except to ensure that the related class is already loaded.
i believe that’s true. In these cases it’s advisable to use
require_dependency rather than require as by using require you are
side stepping Rails’ dependency mechanism completely which can lead to
odd and hard to track down problems.

Fre