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
--
Posted via http://www.ruby-forum.com/.
Can you post a dump of the YAML that''s being stored in your ''emails'' column? Cheers, -DF On 4/21/06, Ed Lau <ted_spam@mac.com> 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). > > 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 > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
David Felstead wrote:> Can you post a dump of the YAML that''s being stored in your ''emails'' > column? > > Cheers, > > -DFSure, this is a sample: "--- \n- !ruby/object:TmEmail \n email: ed@mac.com\n label: home\n" Thanks in advance, --Ed -- Posted via http://www.ruby-forum.com/.
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 On 4/21/06, Ed Lau <ted_spam@mac.com> wrote:> 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 > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
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? -- Posted via http://www.ruby-forum.com/.
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 -- Posted via http://www.ruby-forum.com/.