# Mystified by ActiveRecord.serialize

**URL:** <https://www.ruby-forum.com/t/mystified-by-activerecord-serialize/58353>\
**Category:** Rails\
**Created:** [April 20, 2006, 8:58pm UTC](https://www.ruby-forum.com/t/mystified-by-activerecord-serialize/58353 "2006-04-20T20:58:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![edlau](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b19c9b/32.png) [@edlau](https://www.ruby-forum.com/u/edlau)\
**Post date:** [April 20, 2006, 8:58pm UTC](https://www.ruby-forum.com/t/mystified-by-activerecord-serialize/58353/1 "2006-04-20T20:58:42Z")

</div>

I’m either missing a step, confused or seeing some odd behavior with  
ActiveRecord’s serialize feature. When I load an ActiveRecord object  
from the database, my serialized attribute is a YAML::Object, not the  
original object. My code is based on an example of this capability in  
the “Agile Web Dev. w/ Rails” book (p. 196).

In create.sql:  
`
create table contacts (
…
emails text,
…
);
`

In contact.rb:  
`
class Contact < ActiveRecord::Base
…`

# this is an Array of TmEmails

serialize :emails  
…

# example that illustrates the problem.

# add one email to this contact

def example  
contact = Contact.new(params[:contact])  
emails = []  
home\_email = TmEmail.new(params[:home\_email])  
if !home\_email.empty?  
emails \<\< home\_email  
end  
contact.emails = emails  
…  
end  
end

The problem:  
When my contact object is in memory, the contact.emails looks like this:  
[#\<TmEmail:0x240b8a4 @label=“home”, @email=“removed\_email\_address@domain.invalid”\>]. That’s what  
I expect. However, after I save the contact and read it back in from the  
db, I get this: [#\<YAML::Object:0x2472ce8 @ivars={“label”=\>“home”,  
“email”=\>“removed\_email\_address@domain.invalid”}, @class=“TmEmail”\>]. My code walks the emails  
array expecting TmEmail objects, and I get an exception because I’m  
getting the YAML object instead.

As a test, I serialized the same array by calling to\_yaml and restored  
the object by calling YAML.load(). This worked exactly as expected.

What am I missing?

I’m developing on the Mac:  
Ruby version - 1.8.4 (i686-darwin8.5.1)  
Rails version - 1.0.0

Thanks!  
–Ed

---

<div class="post-metadata">

**Author:** ![edlau](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b19c9b/32.png) [@edlau](https://www.ruby-forum.com/u/edlau)\
**Post date:** [April 21, 2006, 2:01am UTC](https://www.ruby-forum.com/t/mystified-by-activerecord-serialize/58353/2 "2006-04-21T02:01:11Z")

</div>

Can you post a dump of the YAML that’s being stored in your ‘emails’  
column?

Cheers,

-DF

---

<div class="post-metadata">

**Author:** ![edlau](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b19c9b/32.png) [@edlau](https://www.ruby-forum.com/u/edlau)\
**Post date:** [April 21, 2006, 3:27am UTC](https://www.ruby-forum.com/t/mystified-by-activerecord-serialize/58353/3 "2006-04-21T03:27:08Z")

</div>

David F. wrote:

> Can you post a dump of the YAML that’s being stored in your ‘emails’  
> column?
> 
> Cheers,
> 
> -DF

Sure, this is a sample:

“— \n- !ruby/object:TmEmail \n email: removed\_email\_address@domain.invalid\n label: home\n”

Thanks in advance,  
–Ed

---

<div class="post-metadata">

**Author:** ![edlau](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b19c9b/32.png) [@edlau](https://www.ruby-forum.com/u/edlau)\
**Post date:** [June 4, 2006, 5:57pm UTC](https://www.ruby-forum.com/t/mystified-by-activerecord-serialize/58353/4 "2006-06-04T17:57:57Z")

</div>

Ed Lau wrote:

> I’m either missing a step, confused or seeing some odd behavior with  
> ActiveRecord’s serialize feature. When I load an ActiveRecord object  
> from the database, my serialized attribute is a YAML::Object, not the  
> original object. My code is based on an example of this capability in  
> the “Agile Web Dev. w/ Rails” book (p. 196).

Ed - I’m having exactly the same problem. Did you find a solution?

---

<div class="post-metadata">

**Author:** ![edlau](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b19c9b/32.png) [@edlau](https://www.ruby-forum.com/u/edlau)\
**Post date:** [April 21, 2006, 11:54pm UTC](https://www.ruby-forum.com/t/mystified-by-activerecord-serialize/58353/5 "2006-04-21T23:54:43Z")

</div>

In your application.rb, try doing a ‘require\_dependency  
“whatever\_file\_tm\_email\_is\_in”’ - there’s a chance that Rails class  
loader isn’t finding the class in time, though it’s a long shot.

Cheers,

-DF

---

<div class="post-metadata">

**Author:** ![edlau](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b19c9b/32.png) [@edlau](https://www.ruby-forum.com/u/edlau)\
**Post date:** [June 4, 2006, 7:47pm UTC](https://www.ruby-forum.com/t/mystified-by-activerecord-serialize/58353/6 "2006-06-04T19:47:40Z")

</div>

Bill Roberts wrote:

> Ed Lau wrote:
> 
> > I’m either missing a step, confused or seeing some odd behavior with  
> > ActiveRecord’s serialize feature. When I load an ActiveRecord object  
> > from the database, my serialized attribute is a YAML::Object, not the  
> > original object. My code is based on an example of this capability in  
> > the “Agile Web Dev. w/ Rails” book (p. 196).
> 
> Ed - I’m having exactly the same problem. Did you find a solution?

Hi Bill,

No, never did find a solution. I ended up just using my own  
serialization format. If you do figure it out, please let me know.  
Thanks.

–Ed
