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. <biological-metric> <biomass type="float">0.0</biomass> <climate>Temperate</climate> <country>Canada</country> <density type="float">11.0</density> <diversity type="float">0.0</diversity> <geometry type="geometry"> #<GeoRuby::SimpleFeatures::Point:0x34b4238> </geometry> <id type="integer">2</id> <organism-size type="float">-1.0</organism-size> <reserve-name>Ecological reserve</reserve-name> </biological-metric> 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"?> <point> <latitude>-123.333297729</latitude> <longitude>49.0</longitude> </point> 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.
on 2007-06-02 03:51
on 2009-01-29 01:15
On Jun 1 2007, 6:51 pm, Chad B. <firstname.lastname@example.org> 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?