Mystified by ActiveRecord.serialize


#1

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


#2

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

Cheers,

-DF


#3

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


#4

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?


#5

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


#6

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