Override to_xml to collect ActiveRecord objects

Well, I know that you can override the to_xml method for a single instance of an ActiveRecord object, and it works great for me. But how could I override the to_xml method for collecting objects?

Suppose for an instance of the Task model I used to_xml, which looks like this.

def to_xml
  super(:methods => [:tag_list], :include => {:project => {:include => {:folder => {}}}, :folder => {}})
end

Works well when one task needs to be serialized in xml. But when my code starts to collect tasks, as in the following code fragment

render :xml => @tasks.to_xml

I get

wrong number of arguments (1 for 0)

/home/chirantan/.gem/ruby/1.8/gems/activesupport-2.3.5/lib/active_support/core_ext/array/conversions.rb:189:in `to_xml'
/home/chirantan/.gem/ruby/1.8/gems/activesupport-2.3.5/lib/active_support/core_ext/array/conversions.rb:189:in `to_xml'
/home/chirantan/.gem/ruby/1.8/gems/activesupport-2.3.5/lib/active_support/core_ext/array/conversions.rb:189:in `each'
/home/chirantan/.gem/ruby/1.8/gems/activesupport-2.3.5/lib/active_support/core_ext/array/conversions.rb:189:in `to_xml'
/var/lib/gems/1.8/gems/builder-2.1.2/lib/builder/xmlbase.rb:134:in `call'
/var/lib/gems/1.8/gems/builder-2.1.2/lib/builder/xmlbase.rb:134:in `_nested_structures'
/var/lib/gems/1.8/gems/builder-2.1.2/lib/builder/xmlbase.rb:58:in `method_missing'
/var/lib/gems/1.8/gems/builder-2.1.2/lib/builder/xmlbase.rb:31:in `tag!'
/~/blah/app/controllers/tasks_controller.rb:412:in `completed'

How do I do this job?

+3
source share
2 answers

Your override to_xml must be declared

def to_xml options={}
   ....
end
+3
source

, :

render :xml => @tasks

to_xml?

0

Source: https://habr.com/ru/post/1742455/


All Articles