[bug?] XmlSerializer's add_includes should(?) use the association name, not the class name

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

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:

Yeah, I think it's safe to put this in for 2.0, it's come up a bunch of times and the current behaviour is really counter intuitive. I believe I have a patch assigned to me which does this.

This is fixed in trunk. Please let me know if there were any strange side-effects.

Looks good; nice catch that XmlSerializer#to_s dasherizes options[:root].

Nick