I was fooling around with to_xml(:include => { ... }) today, and found that to_xml uses the plural-underscored class name of the association's members instead of the association name:
class Event < ActiveRecord::Base has_many :memberships has_many :users, :through => :memberships has_many :owners, :through => :memberships, :source => :user, :conditions => "event_memberships.`group` = 'owner'" # ignoring append issues for brevity end
Now, if I decide to do this:
event.to_xml(:include => [:users, :owners]), I have an issue: two <users> elements. Similarly, if I had a has_many :participants where group was "participant," I'd expect <owners> and <participants>, and not two <users>.
I'd suggest this modification to activerecord/lib/active_record/ xml_serialization.rb: line 207 tag = records.first.class.to_s.underscore.pluralize should become tag = association.to_s
What do you guys think? I might also go so far as to say that the individual association members should be <user>s instead of <owner>s, which would mean that line 211 records.each { |r| r.to_xml(opts.merge(:root => association.to_s.singularize)) } should become records.each { |r| s = r.class.to_s.underscore; r.to_xml(opts.merge(:root => (dasherize? ? s.dasherize : s))) }
Then, event.to_xml(:include => [:users, :owners, :participants]) would be:
<event> ... <users> <user> ... </user> ... </users> <owners> <user>...</user>... </owners> <participants> <user>...</user>... </participants> </event>
Which makes way more sense to me.
Nick