ROXML from_xml not returning an object instance...


I have two ActiveRecord classes. One is Route the other is Waypoints.
I have constructed an XML representation so I can use AJAX to create a
Route with all waypoints in a single create call to RouteController.
When I call Route.from_xml(xmlString) it is not returning an object -
it's leaving the object (@route) nil, but not erroring. Any ideas?
Here's some code:


  def create
    @route = nil;
    if params[:requestXML]

      # unserialise from XML using ROXML
      @route = Route.from_xml(params[:requestXML]);

      @route =[:route])

    respond_to do |format|

     #The following line unsurprisingly fails, as @route is nil

        flash[:notice] = 'Route was successfully created.'
        format.html { redirect_to(@route) }
        format.xml { render :xml => @route, :status
=> :created, :location => @route }
        format.html { render :action => "new" }
        format.xml { render :xml => @route.errors, :status
=> :unprocessable_entity }


class Route < ActiveRecord::Base
  include ROXML

  has_many :waypoints

  xml_reader :title
  xml_reader :totalDist
  xml_reader :totalMis
  xml_reader :totalHg
  xml_reader :lonlatx
  xml_reader :lonlaty
  xml_reader :grcenter

  xml_accessor :waypoints, [Waypoint], :in => "waypoints"


class Waypoint < ActiveRecord::Base
  include ROXML

  belongs_to :route

  xml_reader :isLeg
  xml_reader :lonlatx
  xml_reader :lonlaty
  xml_reader :gridReference
  xml_reader :ascent
  xml_reader :descent
  xml_reader :distance
  xml_reader :bearing
  xml_reader :timemins

NB I've not specified types at the moment for the above elements. I'm
just trying to verify that the XML is being parsed by ROXML's from_xml

Error Message in logs:

Processing RoutesController#create (for at 2009-02-03
16:36:12) [POST]
  Session ID:
  Parameters: {"requestXML"=>"<route><totalDist>11185.321521477119</

<grcenter>SH 71635 57865</
<lonlaty>271650</lonlaty><gridReference>SH 71650 57290</
<gridReference>SH 74600 57260</gridReference><ascent>275</
<gridReference>SH 73330 59160</gridReference><ascent>73</
<gridReference>SH 70050 59170</gridReference><ascent>182</
<gridReference>SH 69740 57470</gridReference><ascent>29</
<gridReference>SH 70440 56840</gridReference><ascent>640</

"action"=>"create", "controller"=>"routes"}
[globalite] loading locale: en-US from config
[globalite] Locale set to en-US
[globalite] loading locale: en-US from config
[globalite] Locale set to en-US

NoMethodError (You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.delete):
transactions.rb:124:in `rollback_active_record_state!'
transactions.rb:106:in `save'
    /app/controllers/routes_controller.rb:52:in `create'
mime_responds.rb:106:in `call'
mime_responds.rb:106:in `respond_to'
    /app/controllers/routes_controller.rb:51:in `create'



Hey Adam,

I'll take a look at this later today and get back to you. I
specifically haven't tested intermixing ROXMl and ActiveRecord (as you
have with :waypoints). I suspect the problem is that I'm setting the
instance variable rather than going through the proxy accessor. I'll
post here again when I have that worked out.

Or, if you're so inclined, you can dig into the code yourself. See the
"instance_variable_set" line, line 586 here:

Alright, the problem seems to be that ROXML uses the Object#allocate
to generate objects, and never calls #initialize on those objects,
owing to the fact that you would want to initialize differently in a
world where your attributes are already initialized than in one where
they are not (which is the base #new case). There's a workaround
though, which I haven't tested but which will definitely solve the
immediate problem. That is to implement the #xml_initialize method on
your objects, and in it, call the regular #initialize, like so:

class RoxmlClass < ActiveRecord::Base
  include ROXML
  def self.xml_initialize

#xml_initialize (
InstanceMethods/Construction.html#M000010) is called at the end of
#from_xml, and can be used to do things like call initialize with the
data from your xml attributes, or whatever else.

Hope that helps, and I'll have something which handles this in a
general way out in the near future. Feel free to contact me directly
in the future. You'll find my email on the website:

Alright, I found that this actually required a bit more to work
properly, but if you look at my latest code:,
I have a examples/active_record.rb, and spec/examples/active_record.rb
to show it's working properly.

So install this latest version from my github and you should have
things working. See the history:
for the changes involved.

You should also get some deprecation warnings from this newest code.
I'm preparing for a bit of an API shakeup in 3.0 which should be the
last. In short, all type declaration come via the :as parameter, all
location declarations come via :from and :in, all options come as
separate arguments. The rdocs should be up to date on this.