When to_xml is called with an :include, and if there is also one or more :procs passed in, the procs will be evaluated for the "current record" as well as all "included records" without any provision for differentiating which record the proc is acting on.
For example:
include_content_id = proc do |options| xml = options[:builder]
xml.tag! 'content_id', content_id
end
user.to_xml(:include => [:addresses], :procs => [include_content_id])
In this case, (supposing the value of user.content_id = 1), both the "user" xml *AND* the each of the "addresses" xml will contain a
<content-id>1</content-id>
The same problem occurs when :methods => [:content_id] is used (except that there will be an exception raised in my case, because Address objects do not have a content_id method).
As a work-around for the case of procs, I suggest adding a :record key to the options hash that is passed in to the proc. This record would then contain information about the "current record" and could be used to distinguish which of the included ActiveRecord objects are being XML serialized.
include_content_id = proc do |options| xml = options[:builder]
if options[:record].respond_to? :content_id
xml.tag! 'content_id', options[:record].content_id
end
end
The code necessary to permit this is a very simple change to the XmlSerializer initialize method.