Skip to content
Browse files

Include empty has_many/has_and_belongs_to_many associations as empty …

…array tags when serializing to XML

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7169 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 1241615 commit 049b1440a392ac53a427fecd4500bb839e090be4 Tobias Lütke committed Jul 9, 2007
View
11 activerecord/lib/active_record/xml_serialization.rb
@@ -205,13 +205,10 @@ def add_includes
case @record.class.reflect_on_association(association).macro
when :has_many, :has_and_belongs_to_many
records = @record.send(association).to_a
- unless records.empty?
- 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)) }
- end
+ 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)) }
end
when :has_one, :belongs_to
if record = @record.send(association)
View
8 activerecord/test/xml_serialization_test.rb
@@ -162,4 +162,12 @@ def test_should_not_call_methods_on_associations_that_dont_respond
assert_match %r{^ <label>.*</label>}, xml
assert_no_match %r{^ <label>}, xml
end
+
+ def test_should_include_empty_has_many_as_empty_array
+ authors(:david).posts.delete_all
+ xml = authors(:david).to_xml :include=>:posts, :indent => 0
+
+ assert_match %r{<posts type="array"></posts>}, xml
+ end
+
end

0 comments on commit 049b144

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