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 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=“[email protected]”>]. 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”=>“[email protected]”}, @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
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?
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.
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 forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.