Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Change Base#to_xml to take the name for a container element for an as…

…sociation from the element name, not the first record's class name. Closes #7004

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6654 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 0c960602fe13a05a787b1cad85d61f8a45b08427 1 parent b8e7446
@NZKoz NZKoz authored
View
2  activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Use association name for the wrapper element when using .to_xml. Previous behaviour lead to non-deterministic situations with STI and polymorphic associations. [Koz, jstrachan]
+
* Improve performance of calling .create on has_many :through associations. [evan]
* Improved cloning performance by relying less on exception raising #8159 [Blaine]
View
4 activerecord/lib/active_record/xml_serialization.rb
@@ -204,11 +204,11 @@ def add_includes
when :has_many, :has_and_belongs_to_many
records = @record.send(association).to_a
unless records.empty?
- tag = records.first.class.to_s.underscore.pluralize
+ tag = association.to_s
tag = tag.dasherize if dasherize?
builder.tag!(tag) do
- records.each { |r| r.to_xml(opts.merge(:root => association.to_s.singularize)) }
+ records.each { |r| r.to_xml(opts.merge(:root=>r.class.to_s.underscore)) }
end
end
when :has_one, :belongs_to
View
9 activerecord/test/xml_serialization_test.rb
@@ -133,7 +133,8 @@ def test_should_serialize_yaml
class DatabaseConnectedXmlSerializationTest < Test::Unit::TestCase
fixtures :authors, :posts
# to_xml used to mess with the hash the user provided which
- # caused the builder to be reused
+ # caused the builder to be reused. This meant the document kept
+ # getting appended to.
def test_passing_hash_shouldnt_reuse_builder
options = {:include=>:posts}
david = authors(:david)
@@ -141,4 +142,10 @@ def test_passing_hash_shouldnt_reuse_builder
second_xml_size = david.to_xml(options).size
assert_equal first_xml_size, second_xml_size
end
+
+
+ def test_include_uses_association_name
+ xml = authors(:david).to_xml :include=>:hello_posts, :indent=>0
+ assert(xml.include?(%(<hello_posts><post>)) || xml.include?(%(</post></hello-posts>)))
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.