# Creating/Updating Models from text/xml Requests

**URL:** <https://www.ruby-forum.com/t/creating-updating-models-from-text-xml-requests/162816>\
**Category:** Rails\
**Created:** [March 14, 2009, 2:34am UTC](https://www.ruby-forum.com/t/creating-updating-models-from-text-xml-requests/162816 "2009-03-14T02:34:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![MaggotChild](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7ea924/32.png) [@MaggotChild](https://www.ruby-forum.com/u/MaggotChild)\
**Post date:** [March 14, 2009, 2:34am UTC](https://www.ruby-forum.com/t/creating-updating-models-from-text-xml-requests/162816/1 "2009-03-14T02:34:41Z")

</div>

I’m curious how others would deal with this situation.

In my case, I have:

class Package  
has\_many :deliveries  
end

class Delivery  
belongs\_to :package  
belongs\_to :region  
end

class Region  
has\_many :deliveries  
end

And accept XML requests in the following manner:

 Good Shipment Failure US Unknown FR 

Now, my controller would look something like this ugly beast:

class PackagesController  
def create  
if package = params[:package]  
package = Package.find\_or\_initialize\_by\_name(package)  
if deliveries = package.delete(:deliveries)  
deliveries.each do |d|  
region = d.delete(:region)  
region = Region.find\_or\_create\_by\_name(region[:name])  
break if region.nil?  
package.deliveries.create(region)  
package.deliveries[-1].region = region  
end  
end  
end  
#save and more…  
end  
end

As one can see, this is horrible for several reasons. One of the big  
ones being request validation and error handling.

Overriding package.from\_xml is an option, but I find myself thinking  
that I should be validating against request.body with say a DTD, as  
opposed to picking apart the params hash. (Though I’ll have to pick it  
apart anyways to create the models…)

Then I find my self thinking that creating a course grained validation  
method like a DTD is stupid since I have my fine grain validation  
logic within the given models.

Yes, I’ve heard of ROXML.

Any thoughts?

---

<div class="post-metadata">

**Author:** ![MaggotChild](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7ea924/32.png) [@MaggotChild](https://www.ruby-forum.com/u/MaggotChild)\
**Post date:** [March 14, 2009, 3:06am UTC](https://www.ruby-forum.com/t/creating-updating-models-from-text-xml-requests/162816/2 "2009-03-14T03:06:15Z")

</div>

MaggotChild wrote:

> And accept XML requests in the following manner:
> 
> Good Shipment Failure

If you try this from\_xml…

[75525’s gists · GitHub](http://gist.github.com/75525)

…I think it would clear the make-work out of the way, allowing you to  
focus on  
the validations. Tell me if you get stuck - I wrote it for your exact  
situation,  
but it can’t validate automatically.
