Hi, I'm trying to figure out why I can't dump and load a ActiveRecord
object with YAML.
[...]
Er, why? You already have a serialized representation of it in the
database...or are you trying to do some sort of backup and restore (in
which case you might want to look at yaml_db)?
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
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
How is that possible? respond_to? shouldn't need to be overridden to
take method_missing into account, should it?
> 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
How is that possible? respond_to? shouldn't need to be overridden to
take method_missing into account, should it?
The default implementation of respond_to? doesn't know that although a
method (like a attribute accessor) doesn't exist yet, if you were to
call it then method_missing would create it, so respond_to would
return false, even though you would be able to call the method.
Need is a vague word. At a basic level you don't need to override
respond_to, however there was a strong enough feeling that this wasn't
consistent, especially as you'd do things lile
> 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
How is that possible? respond_to? shouldn't need to be overridden to
take method_missing into account, should it?
The default implementation of respond_to? doesn't know that although a
method (like a attribute accessor) doesn't exist yet, if you were to
call it then method_missing would create it, so respond_to would
return false, even though you would be able to call the method.
Really? I thought I'd used respond_to? in that context successfully.
Perhaps I'm wrong or it was overridden.
Need is a vague word. At a basic level you don't need to override
respond_to, however there was a strong enough feeling that this wasn't
consistent, especially as you'd do things lile