This project is archived and is in readonly mode.

#6744 new

ActiveModel Xml Serialization patch

Reported by jimmiesh | April 26th, 2011 @ 02:22 AM

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

mp ='foo' => 'string', 'bar' => 1)
puts mp.to_xml(:type => 'Person')


<?xml version="1.0" encoding="UTF-8"?>
<person type="Person">
  <foo type="Person">string</foo>
  <bar type="integer">1</bar>

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.

No comments found

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>

People watching this ticket