Module: ActiveModel::Serializers::Xml

Extended by:
ActiveSupport::Concern
Includes:
ActiveModel::Serialization
Included in:
ActiveRecord::Serialization, ActiveResource::Base
Defined in:
activemodel/lib/active_model/serializers/xml.rb

Defined Under Namespace

Classes: Serializer

Instance Method Summary collapse

Methods included from ActiveSupport::Concern

append_features, extended, included

Methods included from ActiveModel::Serialization

#serializable_hash

Instance Method Details

#from_xml(xml) ⇒ Object



165
166
167
168
# File 'activemodel/lib/active_model/serializers/xml.rb', line 165

def from_xml(xml)
  self.attributes = Hash.from_xml(xml).values.first
  self
end

#to_xml(options = {}, &block) ⇒ Object

Returns XML representing the model. Configuration can be passed through options.

Without any options, the returned XML string will include all the model’s attributes. For example:

user = User.find(1)
user.to_xml

<?xml version="1.0" encoding="UTF-8"?>
<user>
  <id type="integer">1</id>
  <name>David</name>
  <age type="integer">16</age>
  <created-at type="datetime">2011-01-30T22:29:23Z</created-at>
</user>

The :only and :except options can be used to limit the attributes included, and work similar to the attributes method.

To include the result of some method calls on the model use :methods.

To include associations use :include.

For further documentation see activerecord/lib/active_record/serializers/xml_serializer.xml.



161
162
163
# File 'activemodel/lib/active_model/serializers/xml.rb', line 161

def to_xml(options = {}, &block)
  Serializer.new(self, options).serialize(&block)
end