I’m trying to use my_model.from_xml() to rehydrate an ActiveRecord
object with associations. When I do this I was getting either
ActiveRecord::AssociationTypeMismatch: HouseOfQuality expected, got
Hash
or
ActiveRecord::AssociationTypeMismatch: HouseOfQuality expected, got
Array
Depending on if the association I had had the type=“array” on the
association element in the xml.
I even tried this:
bob = Bob.find(1)
another_bob = Person.new.from_xml(bob.to_xml)
Here is the what the rails 2.0 release says which I think means I
should be able to do this…what am I doing wrong?
Active Record: XML in, JSON out
Active Record has supported serialization to XML for a while. In 2.0
we’ve added deserialization too, so you can say
Person.new.from_xml(“David”) and get what you’d expect. We’ve also
added serialization to JSON, which supports the same syntax as XML
serialization (including nested associations). Just do person.to_json
and you’re ready to roll.