title: additional attributes in both JSON and XML
body: <p>I am currently trying to incorporate attributes in the API of my Rails app. The use case is simple. I have a User model:</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
    attr_accessible :email
  end
  </code></pre>
  
  <p>I have another model, basically linking the users to an Event:</p>
  
  <pre><code>class UserEvent &lt; ActiveRecord::Base
    belongs_to :user
    belongs_to :event
  end
  </code></pre>
  
  <p>I want to be able to list all users related to an event using the UserEvent model through an API accessible as JSON or XML, and I would like the email of my UserEvent to appear in both XML and JSON dump. </p>
  
  <p><a href="https://stackoverflow.com/questions/5162444/want-consistent-behavior-in-to-xml-and-to-json">This question</a> suggests that I can just override serialiable_hash, well this appears to work only for JSON, as it looks like serializable_hash is not used by to_xml</p>
  
  <p>Another approach I have investigated was to override the attributes method in my class:</p>
  
  <pre><code>class UserEvent &lt; ActiveRecord::Base
     def attributes
      @attributes = @attributes.merge "email" =&gt; self.email
      @attributes
    end
  end
  </code></pre>
  
  <p>This works well for JSON, but throws an error when trying the XML version:</p>
  
  <pre><code>undefined method `xmlschema' for "2011-07-12 07:20:50.834587":String
  </code></pre>
  
  <p>This string turns out to be the "created_at" attribute of my object. So it looks like I am doing something wrong on the hash I am manipulating here.</p>