Skip to content
This repository
Browse code

to_xml with :include should skip_instruct on the included records [#4506

state:resolved]

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information...
commit 60504e62c8e2f5e137a0ac82aed67a6c0fe42447 1 parent a003a39
Lawrence Pit lawrencepit authored josevalim committed
2  activerecord/lib/active_record/serializers/xml_serializer.rb
@@ -197,7 +197,7 @@ def add_includes
197 197 # TODO This can likely be cleaned up to simple use ActiveSupport::XmlMini.to_tag as well.
198 198 def add_associations(association, records, opts)
199 199 association_name = association.to_s.singularize
200   - merged_options = options.merge(opts).merge!(:root => association_name)
  200 + merged_options = options.merge(opts).merge!(:root => association_name, :skip_instruct => true)
201 201
202 202 if records.is_a?(Enumerable)
203 203 tag = ActiveSupport::XmlMini.rename_key(association.to_s, options)
7 activerecord/test/cases/serialization_test.rb
@@ -44,4 +44,11 @@ def test_serialize_should_allow_attribute_except_filtering
44 44 assert_equal @contact_attributes[:awesome], contact.awesome, "For #{format}"
45 45 end
46 46 end
  47 +
  48 + def test_serialize_should_xml_skip_instruct_for_included_records
  49 + @contact.alternative = Contact.new(:name => 'Copa Cabana')
  50 + @serialized = @contact.to_xml(:include => [ :alternative ])
  51 + assert_equal @serialized.index('<?xml '), 0
  52 + assert_nil @serialized.index('<?xml ', 1)
  53 + end
47 54 end
4 activerecord/test/models/contact.rb
@@ -13,4 +13,6 @@ def self.column(name, sql_type = nil, options = {})
13 13 column :preferences, :string
14 14
15 15 serialize :preferences
16   -end
  16 +
  17 + belongs_to :alternative, :class_name => 'Contact'
  18 +end

0 comments on commit 60504e6

Please sign in to comment.
Something went wrong with that request. Please try again.