Hello, 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: RoutesController: def create @route = nil; if params[:requestXML] # unserialise from XML using ROXML @route = Route.from_xml(params[:requestXML]); else @route = Route.new(params[:route]) end respond_to do |format| #The following line unsurprisingly fails, as @route is nil if @route.save flash[:notice] = ''Route was successfully created.'' format.html { redirect_to(@route) } format.xml { render :xml => @route, :status => :created, :location => @route } format.js else format.html { render :action => "new" } format.xml { render :xml => @route.errors, :status => :unprocessable_entity } end end end Route.rb: 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" end Waypoint.rb: 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 end 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 method. Error Message in logs: Processing RoutesController#create (for 127.0.0.1 at 2009-02-03 16:36:12) [POST] Session ID: BAh7DToMY3NyZl9pZCIlZWY5NWRjODFjMTI5ZDg3ZDlhZGRjODM0ODA3NmZl YTE6DW5hdnN0YWNrWwYiBi86DnJldHVybl90byIaL2FkYW1mb3dsZXIvZGFz aGJvYXJkOhBsYXN0X2FjdGl2ZVU6IEFjdGl2ZVN1cHBvcnQ6OlRpbWVXaXRo Wm9uZVsISXU6CVRpbWUNb0MbgAAAwMEGOh9AbWFyc2hhbF93aXRoX3V0Y19j b2VyY2lvblQiCFVUQ0AMOgl1c2VyaRM6C2ZvcnVtc3sAOgt0b3BpY3NADiIK Zmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxhc2g6OkZsYXNoSGFzaHsG Ogtub3RpY2UiJFJvdXRlIHdhcyBzdWNjZXNzZnVsbHkgY3JlYXRlZC4GOgpA dXNlZHsGOxBU--48a6d44f6baa5129fd1160815273a9c30ca746a8 Parameters: {"requestXML"=>"<route><totalDist>11185.321521477119</ totalDist><totalHg>640</totalHg><totalMins>235.75000000000003</ totalMins><lonlatx>357865</lonlatx><lonlaty>271635</ lonlaty><grcenter>SH 71635 57865</ grcenter><waypoints><waypoint><isLeg>false</isLeg><lonlatx>357290</ lonlatx><lonlaty>271650</lonlaty><gridReference>SH 71650 57290</ gridReference><ascent>81</ascent><descent>220</descent><distance>0</ distance><bearing>0</bearing></waypoint><waypoint><isLeg>false</ isLeg><lonlatx>357260</lonlatx><lonlaty>274600</ lonlaty><gridReference>SH 74600 57260</gridReference><ascent>275</ ascent><descent>48</descent><distance>2950.152538429157</ distance><bearing>91</bearing></waypoint><waypoint><isLeg>false</ isLeg><lonlatx>359160</lonlatx><lonlaty>273330</ lonlaty><gridReference>SH 73330 59160</gridReference><ascent>73</ ascent><descent>170</descent><distance>2285.3664913969487</ distance><bearing>326</bearing></waypoint><waypoint><isLeg>false</ isLeg><lonlatx>359170</lonlatx><lonlaty>270050</ lonlaty><gridReference>SH 70050 59170</gridReference><ascent>182</ ascent><descent>172</descent><distance>3280.015243867016</ distance><bearing>270</bearing></waypoint><waypoint><isLeg>false</ isLeg><lonlatx>357470</lonlatx><lonlaty>269740</ lonlaty><gridReference>SH 69740 57470</gridReference><ascent>29</ ascent><descent>107</descent><distance>1728.0335644888382</ distance><bearing>190</bearing></waypoint><waypoint><isLeg>false</ isLeg><lonlatx>356840</lonlatx><lonlaty>270440</ lonlaty><gridReference>SH 70440 56840</gridReference><ascent>640</ ascent><descent>717</descent><distance>941.7536832951597</ distance><bearing>132</bearing></waypoint></waypoints></route>", "authenticity_token"=>"a538a4ed8056e17ded2904abfb00628ff6594a07", "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): /Library/Ruby/Gems/1.8/gems/activerecord-2.1.2/lib/active_record/ transactions.rb:124:in `rollback_active_record_state!'' /Library/Ruby/Gems/1.8/gems/activerecord-2.1.2/lib/active_record/ transactions.rb:106:in `save'' /app/controllers/routes_controller.rb:52:in `create'' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.2/lib/action_controller/ mime_responds.rb:106:in `call'' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.2/lib/action_controller/ mime_responds.rb:106:in `respond_to'' /app/controllers/routes_controller.rb:51:in `create'' ... Thanks, Adam. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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: http://github.com/Empact/roxml/blob/c9a3d82827d6939fcc76fa354a52f8bc357ba169/lib/roxml.rb -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 initialize end end #xml_initialize (http://roxml.rubyforge.org/rdoc/classes/ROXML/ 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: http://empact.github.com/roxml/ On Feb 4, 9:33 am, Ben Woosley <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hey Adam, > > I''ll take a look at this later today and get back to you. I > specifically haven''t tested intermixingROXMland 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:http://github.com/Empact/roxml/blob/c9a3d82827d6939fcc76fa354a52f8bc3... > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Alright, I found that this actually required a bit more to work properly, but if you look at my latest code: http://github.com/Empact/roxml/tree/master, 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: http://github.com/Empact/roxml/blob/a2434d5c14a31ec2099c38a6248b4fcf4e5f5565/History.txt 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. On Feb 5, 1:42 am, Ben Woosley <ben.woos...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Alright, the problem seems to be thatROXMLuses 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 > includeROXML > def self.xml_initialize > initialize > end > end > > #xml_initialize (http://roxml.rubyforge.org/rdoc/classes/ROXML/ > 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:http://empact.github.com/roxml/ > > On Feb 4, 9:33 am, Ben Woosley <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: > > > Hey Adam, > > > I''ll take a look at this later today and get back to you. I > > specifically haven''t tested intermixingROXMland 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:http://github.com/Empact/roxml/blob/c9a3d82827d6939fcc76fa354a52f8bc3... > > -- > > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---