# ActiveRecord to\_xml not calling to\_xml on custom class

**URL:** <https://www.ruby-forum.com/t/activerecord-to-xml-not-calling-to-xml-on-custom-class/105746>\
**Category:** Rails\
**Created:** [June 2, 2007, 1:51am UTC](https://www.ruby-forum.com/t/activerecord-to-xml-not-calling-to-xml-on-custom-class/105746 "2007-06-02T01:51:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![underbluewaters](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/5daacb/32.png) [@underbluewaters](https://www.ruby-forum.com/u/underbluewaters)\
**Post date:** [June 2, 2007, 1:51am UTC](https://www.ruby-forum.com/t/activerecord-to-xml-not-calling-to-xml-on-custom-class/105746/1 "2007-06-02T01:51:35Z")

</div>

I’m using the GeoRuby and Spatial Adapter to do some postgis stuff with  
rails. Working great. The thing is, these define custom types that get  
stored on these activerecord objects. Things like  
GeoRuby::SimpleFeatures::Point. So when I call to\_xml everything is  
peachy except for that datatype.

 0.0 Temperate Canada 11.0 0.0 # 2 -1.0 Ecological reserve

So what I did was define to\_json and to\_xml methods for these types.  
They work fine.

> > BiologicalMetric.find(:first).geometry.to\_xml  
> > =\>

\<?xml version="1.0" encoding="UTF-8"?\> -123.333297729 49.0

So I thought that this would solve all my problems and even be something  
I could contribute to the georuby library. Very DRY eh? Wrong!!!  
ActiveRecord doesn’t seem to call the to\_xml method on these datatypes.  
It just does it’s own thing. The JSON serialization ignores the custom  
methods as well.

Am I missing something? Why wouldn’t to\_xml/to\_json be called when the  
parent object’s to\_xml/to\_json method is called?

Thanks,  
Chad B.

---

<div class="post-metadata">

**Author:** ![underbluewaters](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/5daacb/32.png) [@underbluewaters](https://www.ruby-forum.com/u/underbluewaters)\
**Post date:** [January 29, 2009, 12:15am UTC](https://www.ruby-forum.com/t/activerecord-to-xml-not-calling-to-xml-on-custom-class/105746/2 "2009-01-29T00:15:22Z")

</div>

On Jun 1 2007, 6:51 pm, Chad B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:  
[…]

> Am I missing something? Why wouldn’t to\_xml/to\_json be called when the  
> parent object’sto\_xml/to\_json method is called?

1.5 years later I’m experiencing this problem. I found out that as  
opposed to ActiveRecord, ActiveResource uses Hash#to\_xml. Hash#to\_xml  
is in fact written properly, calling to\_xml of each value if method  
found. ActiveRecord however uses XmlSerializer’s logic which in turn  
asks for #to\_s instead of #to\_xml from each attribute. You’d think “I  
can just alias to\_s to\_xml in my class”, but no. XmlSerializer escapes  
whatever it reads from to\_s.

Why? Is XmlSerializer broken?
