aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activerecord/lib/active_record/xml_serialization.rb8
-rw-r--r--activerecord/test/xml_serialization_test.rb5
2 files changed, 9 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/xml_serialization.rb b/activerecord/lib/active_record/xml_serialization.rb
index 5f7a39d73b..42fe46bca3 100644
--- a/activerecord/lib/active_record/xml_serialization.rb
+++ b/activerecord/lib/active_record/xml_serialization.rb
@@ -207,8 +207,12 @@ module ActiveRecord #:nodoc:
records = @record.send(association).to_a
tag = association.to_s
tag = tag.dasherize if dasherize?
- builder.tag!(tag, :type => :array) do
- records.each { |r| r.to_xml(opts.merge(:root=>r.class.to_s.underscore)) }
+ if records.empty?
+ builder.tag!(tag, :type => :array)
+ else
+ builder.tag!(tag, :type => :array) do
+ records.each { |r| r.to_xml(opts.merge(:root=>r.class.to_s.underscore)) }
+ end
end
when :has_one, :belongs_to
if record = @record.send(association)
diff --git a/activerecord/test/xml_serialization_test.rb b/activerecord/test/xml_serialization_test.rb
index 824f486d2e..807daddabb 100644
--- a/activerecord/test/xml_serialization_test.rb
+++ b/activerecord/test/xml_serialization_test.rb
@@ -165,9 +165,10 @@ class DatabaseConnectedXmlSerializationTest < Test::Unit::TestCase
def test_should_include_empty_has_many_as_empty_array
authors(:david).posts.delete_all
- xml = authors(:david).to_xml :include=>:posts, :indent => 0
+ xml = authors(:david).to_xml :include=>:posts, :indent => 2
- assert_match %r{<posts type="array"></posts>}, xml
+ assert_equal [], Hash.from_xml(xml)['author']['posts']
+ assert_match %r{^ <posts type="array"/>}, xml
end
end \ No newline at end of file