# Seeking optimization help - overridden to\_xml()

**URL:** <https://www.ruby-forum.com/t/seeking-optimization-help-overridden-to-xml/171831>\
**Category:** Rails\
**Created:** [July 9, 2009, 9:21pm UTC](https://www.ruby-forum.com/t/seeking-optimization-help-overridden-to-xml/171831 "2009-07-09T21:21:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![michael\_teter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e47c2d/32.png) [@michael\_teter](https://www.ruby-forum.com/u/michael_teter)\
**Post date:** [July 9, 2009, 9:21pm UTC](https://www.ruby-forum.com/t/seeking-optimization-help-overridden-to-xml/171831/1 "2009-07-09T21:21:02Z")

</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  
…  
def to\_xml(options = {})  
options[:indent] ||= 2  
xml = options[:builder] ||= Builder::XmlMarkup.new(:indent =\>  
options[:indent])  
xml.instruct! unless options[:skip\_instruct]  
xml.error do |x|  
x.id(self.id) if self.attributes.has\_key?  
(“id”)  
x.division\_name(self.division\_name) if self.attributes.has\_key?  
(“division\_name”)  
x.division\_id(self.division\_id) if self.attributes.has\_key?  
(“division\_id”)  
x.region\_name(self.region\_name) if self.attributes.has\_key?  
(“region\_name”)  
x.region\_id(self.region\_id) if self.attributes.has\_key?  
(“region\_id”)  
x.market\_name(self.market\_name) if self.attributes.has\_key?  
(“market\_name”)  
x.market\_id(self.market\_id) if self.attributes.has\_key?  
(“market\_id”)  
x.vp\_name(self.vp\_name) if self.attributes.has\_key?  
(“vp\_name”)  
x.vp\_id(self.vp\_id) if self.attributes.has\_key?  
(“vp\_id”)  
x.dir\_name(self.dir\_name) if self.attributes.has\_key?  
(“dir\_name”)  
x.dir\_id(self.dir\_id) if self.attributes.has\_key?  
(“dir\_id”)  
x.mgr\_name(self.mgr\_name) if self.attributes.has\_key?  
(“mgr\_name”)  
x.mgr\_id(self.mgr\_id) if self.attributes.has\_key?  
(“mgr\_id”)  
x.sup\_name(self.sup\_name) if self.attributes.has\_key?  
(“sup\_name”)  
x.sup\_id(self.sup\_id) if self.attributes.has\_key?  
(“sup\_id”)  
x.cae\_name(self.cae\_name) if self.attributes.has\_key?  
(“cae\_name”)  
x.cae\_id(self.cae\_id) if self.attributes.has\_key?  
(“cae\_id”)  
x.acct(self.acct) if self.attributes.has\_key?  
(“acct”)  
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"))

```
  x.crtd(self.crtd ? self.crtd.to_date.to_s(:db) : "")

```

if self.attributes.has\_key?(“crtd”)  
x.schd(self.schd ? self.schd.to\_date.to\_s(:db) : “”)  
if self.attributes.has\_key?(“schd”)  
x.run\_date(self.run\_date ? self.run\_date.to\_date.to\_s(:db) : “”)  
if self.attributes.has\_key?(“run\_date”)  
x.errtype(self.errtype) if self.attributes.has\_key?  
(“errtype”)  
x.waiver(self.waiver) if self.attributes.has\_key?  
(“waiver”)  
x.stream(self.stream) if self.attributes.has\_key?  
(“stream”)  
x.disposition(self.disposition) if self.attributes.has\_key?  
(“disposition”)  
x.biller(self.biller) if self.attributes.has\_key?  
(“biller”)  
x.corp(self.corp) if self.attributes.has\_key?  
(“corp”)  
x.alert(self.alert) if self.attributes.has\_key?  
(“alert”)  
x.audit\_classification(self.audit\_classification) if  
self.attributes.has\_key?(“audit\_classification”)  
x.audit\_category(self.audit\_category) if  
self.attributes.has\_key?(“audit\_category”)  
end  
end  
…  
end

THANKS!

---

<div class="post-metadata">

**Author:** ![michael\_teter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e47c2d/32.png) [@michael\_teter](https://www.ruby-forum.com/u/michael_teter)\
**Post date:** [July 9, 2009, 10:31pm UTC](https://www.ruby-forum.com/t/seeking-optimization-help-overridden-to-xml/171831/2 "2009-07-09T22:31:45Z")

</div>

On Jul 9, 8:20 pm, michael\_teter [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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
