When passing a :type option to to_xml, the passed type is
propagated to all string attributes in the ActiveModel. For
instance:
class Person
include ActiveModel::Serializers::Xml
extend ActiveModel::Naming
attr_accessor :attributes
def initialize(attributes)
@attributes = attributes
end
end
mp = Person.new('foo' => 'string', 'bar' => 1)
puts mp.to_xml(:type => 'Person')
produces:
<?xml version="1.0" encoding="UTF-8"?>
<person type="Person">
<foo type="Person">string</foo>
<bar type="integer">1</bar>
</person>
We ran into this because ActiveRecord adds a type option when
serializing has_many associations where the association class does
not match the association name. For instance:
has_many :cars, :class_name => 'Automobile'
In this case, we wanted the type attribute on each car in the
xml, but we don't want it on the car's string attributes :).
Please consider the attached patch for inclusion in the next
release of Rails. Thanks.