# Serializing objects to database

**URL:** <https://www.ruby-forum.com/t/serializing-objects-to-database/141135>\
**Category:** Rails\
**Created:** [June 22, 2008, 12:27pm UTC](https://www.ruby-forum.com/t/serializing-objects-to-database/141135 "2008-06-22T12:27:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [June 22, 2008, 12:27pm UTC](https://www.ruby-forum.com/t/serializing-objects-to-database/141135/1 "2008-06-22T12:27:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [June 22, 2008, 4:27pm UTC](https://www.ruby-forum.com/t/serializing-objects-to-database/141135/2 "2008-06-22T16:27:12Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [June 22, 2008, 6:34pm UTC](https://www.ruby-forum.com/t/serializing-objects-to-database/141135/3 "2008-06-22T18:34:02Z")

</div>

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
