Reconstructing model objects from XML

Hi

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

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

    m = Model.new(hash)

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

    class Account       has_many :users     end

The XML produced by to_xml is:

    <account>       <id>1</id>       <users>         <user>           <id>1</1>         </user>       </users>     </account>

Calling Account.new(hash) 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.