Forum: Ruby on Rails Mystified by ActiveRecord.serialize

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
311e09a61c1d72c25c99d8f6bb580d28?d=identicon&s=25 Ed Lau (edlau)
on 2006-04-20 20:58
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:
<code>
create table contacts (
  ...
  emails text,
  ...
);
</code>

In contact.rb:
<code>
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
</code>

The problem:
When my contact object is in memory, the contact.emails looks like this:
 [#<TmEmail:0x240b8a4 @label="home", @email="foo@aol.com">]. 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"=>"foo@aol.com"}, @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
6c27f78ab0eee78732ae54e8b8718b84?d=identicon&s=25 David Felstead (Guest)
on 2006-04-21 02:01
(Received via mailing list)
Can you post a dump of the YAML that's being stored in your 'emails'
column?

Cheers,

-DF
311e09a61c1d72c25c99d8f6bb580d28?d=identicon&s=25 Ed Lau (edlau)
on 2006-04-21 03:27
David Felstead 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: ed@mac.com\n  label: home\n"

Thanks in advance,
--Ed
6c27f78ab0eee78732ae54e8b8718b84?d=identicon&s=25 David Felstead (Guest)
on 2006-04-21 23:54
(Received via mailing list)
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
47d0bfbb5b81fa04559b0b7790d17106?d=identicon&s=25 Bill Roberts (bill)
on 2006-06-04 17:57
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?
311e09a61c1d72c25c99d8f6bb580d28?d=identicon&s=25 Ed Lau (Guest)
on 2006-06-04 19:47
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
This topic is locked and can not be replied to.