On Jan 27, 9:16 pm, bob <larr...@gmail.com> wrote:
> 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. > At a guess it looks like during the yaml loading process it calls (or causes to be called) respond_to?, however respond_to for an AR object depends on the internal state (because of the dynamically generated methods) of the object, and respond_to is being called when that state is inconsistent (I'd guess that @attributes hasn't been restored yet, so is nil). Changing Dummy to ActiveRecord:Dummy changes things because yaml no longer finds your class and so it doesn't get AR's respond_to (what you change it to is probably irrelevant, as long as you don't change it to some other AR class. I seem to recall that historically, although method_missing etc was overriden to add dynamic methods, respond_to wasn't, which didn't make AR a very good citizen, which may be why this works with projects using old versions of rails Fred > 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.