# Serialization problem

**URL:** <https://www.ruby-forum.com/t/serialization-problem/172192>\
**Category:** Rails\
**Created:** [July 16, 2009, 2:44pm UTC](https://www.ruby-forum.com/t/serialization-problem/172192 "2009-07-16T14:44:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jcernelli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/73ab20/32.png) [@jcernelli](https://www.ruby-forum.com/u/jcernelli)\
**Post date:** [July 16, 2009, 2:44pm UTC](https://www.ruby-forum.com/t/serialization-problem/172192/1 "2009-07-16T14:44:47Z")

</div>

Hi, I’m sure I’m doing something obviously incorrect here, but have  
stared at it for so long I can’t seem to find it.

I want to serialize some activerecord objects as rows in a table.  
Basically I can:

> > import\_object = ImportObject.new  
> > =\> #\<ImportObject id: nil, object: nil, created\_at: nil, updated\_at:  
> > nil\>

> > import\_object.object = Fubar.find(32)  
> > =\> #\<Fubar id: 32, name: “Fubar’d” \>

> > import\_object  
> > =\> #\<ImportObject id: 2958, object: #\<Fubar id: 32, name: “Fubar’d” \>,  
> > created\_at: “2009-07-15 22:42:40”, updated\_at: “2009-07-15 22:42:40”\>

Perfect! Until I save…

> > import\_object.save  
> > =\> true

> > io = ImportObject.find(2958)  
> > =\> #\<ImportObject id: 2958, import\_id: nil, object: 32, created\_at:  
> > “2009-07-15 22:42:40”, updated\_at: “2009-07-15 23:10:11”\>

For some reason the object is getting saved as a fixnum, and I can’t  
figure out why. Any ideas?

Here’s the setup:

class CreateImportObjects \< ActiveRecord::Migration  
def self.up  
create\_table :import\_objects do |t|  
t.string :object, :limit =\> 512  
t.timestamps  
end  
end

def self.down  
drop\_table :import\_objects  
end  
end

class ImportObject \< ActiveRecord::Base  
serialize :object  
end

---

<div class="post-metadata">

**Author:** ![jcernelli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/73ab20/32.png) [@jcernelli](https://www.ruby-forum.com/u/jcernelli)\
**Post date:** [July 16, 2009, 8:16pm UTC](https://www.ruby-forum.com/t/serialization-problem/172192/2 "2009-07-16T20:16:14Z")

</div>

One last thing:

1. You will need to put the "requires " _Anyplace_ you de-  
serialize, this could include a controller unless you like in this  
example:

def create  
import = Import.find(session[:import\_id])

```
@collection = import.objects
import_object = @collection.first
puts import_object
puts import_object.class
@headers = @collection.first.class.import_headers

```

end

Without the requires statement the “puts” give this:  
#YAML::Object:0x10332eea8  
Fubar

So calling any Fubar functions on the de-serialized object will fail.

Add the “requires” and expected behavior results:  
#Fubar:0x10325d5b0  
Fubar

Hopefully done with this

---

<div class="post-metadata">

**Author:** ![jcernelli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/73ab20/32.png) [@jcernelli](https://www.ruby-forum.com/u/jcernelli)\
**Post date:** [July 20, 2009, 3:55pm UTC](https://www.ruby-forum.com/t/serialization-problem/172192/3 "2009-07-20T15:55:14Z")

</div>

Upon closer review… and a weekend to clear my head…

This approach was just flat wrong. Don’t even think of serializing  
activeRecords. It causes all sorts of problems, some you will see,  
some will be beneath the water line.

I found memcached and after reading about it, it is exactly what I’m  
looking for.

---

<div class="post-metadata">

**Author:** ![jcernelli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/73ab20/32.png) [@jcernelli](https://www.ruby-forum.com/u/jcernelli)\
**Post date:** [July 16, 2009, 5:32pm UTC](https://www.ruby-forum.com/t/serialization-problem/172192/4 "2009-07-16T17:32:40Z")

</div>

Solution found for posterity:

In order to serialize active records I had to do two things:

1. Wrap the object in an array (import\_object.object = [fubar])

The problem here, after a save, I was getting a yaml record returned  
instead of a de-serialized object. After much poking around I found  
this excellent article explaining that somehow custom classes  
(including models it seems) were not in scope:

[http://itsignals.cascadia.com.au/?p=10](http://itsignals.cascadia.com.au/?p=10)

In the comments someone made the insightful recommendation to require  
the object being serialized in the class doing the serialization.

1. So, I included a requires statement for any class that I want to  
serialize. (require “Fubar” in the example below).

Hope this saves someone some time!
