# seeking optimization help - overridden to\_xml()

**URL:** <https://discuss.rubyonrails.org/t/seeking-optimization-help-overridden-to-xml/44122>\
**Category:** rubyonrails-talk\
**Created:** [July 9, 2009, 7:20pm UTC](https://discuss.rubyonrails.org/t/seeking-optimization-help-overridden-to-xml/44122 "2009-07-09T19:20:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![blunte](https://avatars.discourse-cdn.com/v4/letter/b/b782af/32.png) [@blunte](https://discuss.rubyonrails.org/u/blunte)\
**Post date:** [July 9, 2009, 7:20pm UTC](https://discuss.rubyonrails.org/t/seeking-optimization-help-overridden-to-xml/44122/1 "2009-07-09T19:20:13Z")

</div>

Howdy.

I've got an overridden to\_xml() function in one of my models that appears to be very slow. As a relative Ruby novice, I would love to hear suggestions on how to improve the performance. Incidentally, I was using the default to\_xml() provided by ActiveRecord, but it wasn't handling null data the way I wanted.

class Errors \< ActiveRecord:Base ... &nbsp;&nbsp;def to\_xml(options = {}) &nbsp;&nbsp;&nbsp;&nbsp;options[:indent] ||= 2 &nbsp;&nbsp;&nbsp;&nbsp;xml = options[:builder] ||= Builder::XmlMarkup.new(:indent =\> options[:indent]) &nbsp;&nbsp;&nbsp;&nbsp;xml.instruct! unless options[:skip\_instruct] &nbsp;&nbsp;&nbsp;&nbsp;xml.error do |x| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.id(self.id) if self.attributes.has\_key? ("id") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.division\_name(self.division\_name) if self.attributes.has\_key? ("division\_name") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.division\_id(self.division\_id) if self.attributes.has\_key? ("division\_id") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.region\_name(self.region\_name) if self.attributes.has\_key? ("region\_name") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.region\_id(self.region\_id) if self.attributes.has\_key? ("region\_id") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.market\_name(self.market\_name) if self.attributes.has\_key? ("market\_name") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.market\_id(self.market\_id) if self.attributes.has\_key? ("market\_id") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.vp\_name(self.vp\_name) if self.attributes.has\_key? ("vp\_name") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.vp\_id(self.vp\_id) if self.attributes.has\_key? ("vp\_id") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.dir\_name(self.dir\_name) if self.attributes.has\_key? ("dir\_name") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.dir\_id(self.dir\_id) if self.attributes.has\_key? ("dir\_id") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.mgr\_name(self.mgr\_name) if self.attributes.has\_key? ("mgr\_name") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.mgr\_id(self.mgr\_id) if self.attributes.has\_key? ("mgr\_id") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.sup\_name(self.sup\_name) if self.attributes.has\_key? ("sup\_name") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.sup\_id(self.sup\_id) if self.attributes.has\_key? ("sup\_id") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.cae\_name(self.cae\_name) if self.attributes.has\_key? ("cae\_name") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.cae\_id(self.cae\_id) if self.attributes.has\_key? ("cae\_id") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.acct(self.acct) if self.attributes.has\_key? ("acct") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.wono(self.wono) if self.attributes.has\_key? ("wono") # x.crtd(self.crtd.strftime("%Y-%m-%d")) # Why wouldn't this have worked?... # perhaps it should have been crtd.to\_date.strftime() ... # x.schd(self.schd.strftime("%Y-%m-%d")) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.crtd(self.crtd ? self.crtd.to\_date.to\_s(:db) : "") if self.attributes.has\_key?("crtd") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.schd(self.schd ? self.schd.to\_date.to\_s(:db) : "") if self.attributes.has\_key?("schd") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.run\_date(self.run\_date ? self.run\_date.to\_date.to\_s(:db) : "") if self.attributes.has\_key?("run\_date") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.errtype(self.errtype) if self.attributes.has\_key? ("errtype") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.waiver(self.waiver) if self.attributes.has\_key? ("waiver") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.stream(self.stream) if self.attributes.has\_key? ("stream") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.disposition(self.disposition) if self.attributes.has\_key? ("disposition") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.biller(self.biller) if self.attributes.has\_key? ("biller") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.corp(self.corp) if self.attributes.has\_key? ("corp") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.alert(self.alert) if self.attributes.has\_key? ("alert") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.audit\_classification(self.audit\_classification) if self.attributes.has\_key?("audit\_classification") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.audit\_category(self.audit\_category) if self.attributes.has\_key?("audit\_category") &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end ... end

THANKS!

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [July 9, 2009, 8:30pm UTC](https://discuss.rubyonrails.org/t/seeking-optimization-help-overridden-to-xml/44122/2 "2009-07-09T20:30:56Z")

</div>

> Howdy.
> 
> I've got an overridden to\_xml() function in one of my models that appears to be very slow. As a relative Ruby novice, I would love to hear suggestions on how to improve the performance. Incidentally, I was using the default to\_xml() provided by ActiveRecord, but it wasn't handling null data the way I wanted.

The attributes method returns a new hash everytime you call it - it might help to only call it once and stash the result in a local variable. You might find the :only option to to\_xml allows you to solve this problem without all this rather repetitive code.

Fred
