Nick Howell
2007-May-02 04:47 UTC
[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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---
Michael Koziarski
2007-May-02 05:13 UTC
Re: [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: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. -- Cheers Koz --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---
Michael Koziarski
2007-May-03 02:29 UTC
Re: [bug?] XmlSerializer''s add_includes should(?) use the association name, not the class name
On 5/2/07, Michael Koziarski <michael@koziarski.com> wrote:> > 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. -- Cheers Koz --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---
Nick Howell
2007-May-03 04:50 UTC
Re: [bug?] XmlSerializer''s add_includes should(?) use the association name, not the class name
Looks good; nice catch that XmlSerializer#to_s dasherizes options[:root]. Nick On May 2, 9:29 pm, "Michael Koziarski" <mich...@koziarski.com> wrote:> On 5/2/07, Michael Koziarski <mich...@koziarski.com> wrote: > > > > 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. > > -- > Cheers > > Koz--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---