Calling to_xml on an Array of ROXML objects results in an empty collection
I have a ROXML object that looks like:
class Activity
include ROXML
xml_accessor :id
end
If I have an array of these objects and call .to_xml on the array, I receive an empty xml collection:
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<activities type=\"array\">\n</activities>\n"
Any idea why or how开发者_开发知识库 to fix this?
I'm running Rails 2.3.5 with the newest version of ROXML.
Similar question: Ruby ROXML - how to get an array to render its xml?. Look there for details.
It seems that it is not possible to convert an array using ROXML. One way to solve this is to define new class for collection:
class Activities
include ROXML
def initialize(activities)
@activities = activities
end
xml_reader :activities, :as => [Activity]
end
And use:
Activities.new(activities).to_xml
精彩评论