Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

ActiveRecord::Serialization class method from_xml handles arrays and singleton XML instances

#3133

I wanted a from_xml method for ActiveRecord that would be able to import data from XML that contained an array of objects as well as just a single object. Also, this should be a class method so it would instantiate the objects for me. The Gist at http://gist.github.com/179530 has my implementation. Is this worth submitting?

Reported by sbwoodside · September 2nd, 2009 @ 04:38 AM

State: stale
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. sbwoodside
    sbwoodside
      module ActiveRecord::Serialization
        def self.from_xml(xml)
          the_class = Hash.from_xml(xml).keys.first.singularize.camelize.constantize # singularize does nothing if already singular
          array_or_hash = Hash.from_xml(xml).values.first
          array_or_hash.class == Array ? array_or_hash.map { |hash| the_class.new( hash ) } : the_class.new( array_or_hash )
        end
      end
    

    September 2nd, 2009 @ 09:48 PM

  2. Rohit Arondekar
    Rohit Arondekar
    • State changed from new to stale
    • Importance changed from to

    Marking ticket as stale. If this is still an issue please leave a comment with suggested changes, creating a patch with tests, rebasing an existing patch or just confirming the issue on a latest release or master/branches.

    October 6th, 2010 @ 06:43 AM