class Array

def to_xml(options = {})




1
2008-03-07T09:58:18+01:00
1
1
2008-03-07T09:58:18+01:00




Message.all.to_xml(:skip_types => true)

The +options+ hash is passed downwards:

singularized. You can change it with the :children option.
By default root children have as node name the one of the root




customer_with_no_projects.projects.to_xml(:root => "projects")

To ensure a meaningful root element use the :root option:




[].to_xml

If the collection is empty the root element is "nil-classes" by default:



3


1
2




[{:foo => 1, :bar => 2}, {:baz => 3}].to_xml

Otherwise the root element is "records":



...
2008-04-15
1567
57230.0


...
2008-04-09
1567
20000.0




customer.projects.to_xml

if all elements belong to the same type and that's not Hash:
The root node reflects the class name of the first element in plural

not an exception is raised.
All elements are expected to respond to +to_xml+, if any of them does

in XML to this method.
to each element. Active Record collections delegate their representation
Returns a string that represents this array in XML by sending +to_xml+
def to_xml(options = {})
  require 'active_support/builder' unless defined?(Builder)
  options = options.dup
  options[:indent]  ||= 2
  options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent])
  options[:root]    ||= if first.class.to_s != "Hash" && all? { |e| e.is_a?(first.class) }
    underscored = ActiveSupport::Inflector.underscore(first.class.name)
    ActiveSupport::Inflector.pluralize(underscored).tr('/', '_')
  else
    "objects"
  end
  builder = options[:builder]
  builder.instruct! unless options.delete(:skip_instruct)
  root = ActiveSupport::XmlMini.rename_key(options[:root].to_s, options)
  children = options.delete(:children) || root.singularize
  attributes = options[:skip_types] ? {} : {:type => "array"}
  return builder.tag!(root, attributes) if empty?
  builder.__send__(:method_missing, root, attributes) do
    each { |value| ActiveSupport::XmlMini.to_tag(children, value, options) }
    yield builder if block_given?
  end
end