On Jan 27, 2011, at 1:16 PM, bob wrote:

> Hi, I'm trying to figure out why I can't dump and load a ActiveRecord
> object with YAML.

Not sure about your error, but you could do...

tom = Dummy.new(:name => 'Tom')
yml = toml.attributes.to_yaml
new_tom = Dummy.new(YAML.load(yml))

or something similar...


> Please see the gist below for how to replicate, and the exact error
> I'm getting.
> 
> https://gist.github.com/84f44da695b218ae9474
> 
> Basically I create a new project, new database, and new model.
> 
> Create an instance of the model, call to_yaml on it, store that, and
> then try and load an object from that.
> 
> This general idea, of reconstituting a model from it's YAML
> representation works in some of my projects, but not in any new
> ones.
> 
> I have no idea why this would be the case.
> 
> Changing the type of the yaml dump from
> !ruby/object:Dummy
> to
> !ruby/ActiveRecord:Dummy
> helps a little bit, but still returns something weird.
> 
> ruby-1.9.2-p0 > tom
> => "--- !ruby/object:Dummy \nattributes: \n  id: 1\n  name: Tim\n
> created_at: 2011-01-27 21:12:33.327345\n  updated_at: 2011-01-27
> 21:12:33.327345\nattributes_cache: {}\n\nchanged_attributes: {}\n
> \ndestroyed: false\nmarked_for_destruction: false\npersisted: true
> \npreviously_changed: {}\n\nreadonly: false\n"
> ruby-1.9.2-p0 > bill = tom.gsub("object", 'ActiveRecord')
> => "--- !ruby/ActiveRecord:Dummy \nattributes: \n  id: 1\n  name: Tim
> \n  created_at: 2011-01-27 21:12:33.327345\n  updated_at: 2011-01-27
> 21:12:33.327345\nattributes_cache: {}\n\nchanged_attributes: {}\n
> \ndestroyed: false\nmarked_for_destruction: false\npersisted: true
> \npreviously_changed: {}\n\nreadonly: false\n"
> ruby-1.9.2-p0 > YAML.load(bill)
> => #<Syck::DomainType:0x00000100c634b8 @domain="ruby.yaml.org,2002",
> @type_id="ActiveRecord:Dummy", @value={"attributes"=>{"id"=>1,
> "name"=>"Tim", "created_at"=>"2011-01-27 21:12:33.327345",
> "updated_at"=>"2011-01-27 21:12:33.327345"}, "attributes_cache"=>{},
> "changed_attributes"=>{}, "destroyed"=>false,
> "marked_for_destruction"=>false, "persisted"=>true,
> "previously_changed"=>{}, "readonly"=>false}>
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Ruby on Rails: Talk" group.
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> To unsubscribe from this group, send email to 
> rubyonrails-talk+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/rubyonrails-talk?hl=en.
> 

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to