to_xml on Hash with string array fails with Not all elements respond to to_xml
>>r={"records"=>["001","002"]}
=> {"records"=>["001", "002"]}
>>r.to_xml
RuntimeError: Not all elements respond
to to_xml from
/jruby/../1.8/gems/activesupport2.3.9/lib/active_support/core_ext/array/conversions.rb:163:in `to_xml'
Is there a rails preferred way to change the Hash.to_xml behavior to return
<records>
<record>001</record>
<record>002</record>
</records>
…
Just like DigitalRoss said, this appears to work out of the box in Ruby 1.9 with ActiveSupport 3:
At least with MRI (you’re using JRuby, though), you can get similar behavior on Ruby 1.8 with ActiveSupport 2.3.9:
Which gives you…
Note that my code doesn’t work with Ruby 1.9 and ActiveRecord 3.