Reconstructing model objects from XML


I’m using to_xml to provide XML export model objects. I’m looking for
a simple solution to implement the reverse, so that I can reconstruct
the model objects from the XML.

Using Hash.from_xml I can parse the XML into a Hash and pass this into
the initializer:

m =

Whilst this works fine for simple models it doesn’t (seemingly) work
with relationships. Take the following example:

class Account
  has_many :users

The XML produced by to_xml is:


Calling results in
“ActiveRecord::AssociationTypeMismatch: User expected, got Array”,
which is to be expected because the initialize method doesn’t
configure relationships in this way (I don’t think).

Does anyone have a proper solution for this problem, perhaps by
extending ActiveRecord, or is it just a matter of manually
reconstructing each of the relationships?

Thanks in advance.