I'm working with a Flex client and a RESTful Rails app which has the 
following models:   

Simulation:  has_many loads, buffers and servers
Server: belongs_to simulation
Load: belongs_to simulation
Buffer: belongs_to simulation

I have created an input form where the user can input all attributes 
of a simulation, server, load and buffer, and then click a Submit 
button.  The action of the Submit button should create a new 
simulation with an associated server, load and buffer.  

The server, load and buffer can't exist independent of a simulation, 
nor can a simulation be processed without at least one each of these 
3 entities.  My initial thought was to use the create method of the 
simulations_controller to create the simulation, server, load and 
buffer.  However, it is necessary to save the simulation first 
before creating the load, buffer and server, and additionally I 
can't figure out how to structure the <mx:request> that is sent with 
the HTTPService to trigger the create method.  If I include the 
<buffer>, for example, in the <simulation></simulation> XML, I get 
an error when the simulations_controller tries to process that field 
while it is creating the Simulation object.  

Another approach I have tried is to call the HTTPService to create 
the simulation first, and then call the services to create the 
server, load and buffer after the simulation is created and I have a 
simulation_id to reference.  However, when I try the latter 
approach, I get one of those nasty 2032 errors:

[FaultEvent fault=[RPC Fault faultString="HTTP request error" 
faultCode="Server.Error.Request" faultDetail="Error: [IOErrorEvent 
type="ioError" bubbles=false cancelable=false eventPhase=2 
text="Error #2032: Stream Error. URL: 
http://localhost:3000/simulations.xml";]. URL: /simulations.xml"] 
messageId="C2E405F1-C735-2E7C-0888-855DBC1D7A00" type="fault" 
bubbles=false cancelable=true eventPhase=2]

The HTTPService call looks like this:

<mx:HTTPService id="svcSimulationCreate" url="/simulations.xml"
     contentType="application/xml" resultFormat="e4x"
      method="POST" result="handleSimulationCreateResult
(event.result as XML)" 
      fault="Alert.show(event.toString())">
      <mx:request xmlns="">
            <simulation>
                <name>{runControlBox.simNameTI.text}</name>
                <desc>{runControlBox.descTI.text}</desc>
                <run_length>{runControlBox.runLengthTI.text}
</run_length>
                <time_units>{runControlBox.getTimeUnits
(runControlBox.timeUnitsCB.selectedIndex)}</time_units>
            </simulation>
      </mx:request>
</mx:HTTPService>

The weird thing is that even though I get this error, the Simulation 
is created in mySQL database – no servers, loads or buffers, just 
the Simulation.  There are no errors on the server window, and the 
logs don't show anything amiss either.  Here is a snippet from my 
development.log:

Processing SimulationsController#create (for 127.0.0.1 at 2008-12-29 
20:44:02) [POST]
  Session ID: 
BAh7BzoMdXNlcl9pZGkIIgpmbGFzaElDOidBY3Rpb25Db250cm9sbGVyOjpG
bGFzaDo6Rmxhc2hIYXNoewAGOgpAdXNlZHsA--
78b034ba15954f07b8790002a454771bc17a9990
  Parameters: {"simulation"=>
{"name"=>"Test424242", "run_length"=>"42", "desc"=>"424242", "time_un
its"=>"hr"}, "format"=>"xml", "action"=>"create", "controller"=>"simu
lations"}
   [4;36;1mUser Columns (0.015000) [0m    [0;1mSHOW FIELDS FROM 
`users` [0m
   [4;35;1mUser Load (0.016000) [0m    [0mSELECT * FROM `users` 
WHERE (`users`.`id` = 3) LIMIT 1 [0m
   [4;36;1mSimulation Columns (0.000000) [0m    [0;1mSHOW FIELDS 
FROM `simulations` [0m
   [4;35;1mSQL (0.000000) [0m    [0mBEGIN [0m
   [4;36;1mSQL (0.000000) [0m    [0;1mSELECT `name` FROM 
`simulations` WHERE (`simulations`.name = 'Test424242' AND 
`simulations`.user_id = 3)  [0m
   [4;35;1mSimulation Create (0.015000) [0m    [0mINSERT INTO 
`simulations` (`name`, `updated_at`, `run_length`, `desc`, 
`time_units`, `user_id`, `created_at`) VALUES('Test424242', '2008-12-
30 01:44:02', 42.0, '424242', 'hr', 3, '2008-12-30 01:44:02') [0m 
   [4;36;1mSQL (0.078000) [0m    [0;1mCOMMIT [0m
   [4;35;1mResource Load (0.000000) [0m    [0mSELECT * FROM 
`resources` WHERE (`resources`.simulation_id = 19)  [0m
   [4;36;1mLoad Load (0.000000) [0m    [0;1mSELECT * FROM `loads` 
WHERE (`loads`.simulation_id = 19)  [0m
Completed in 0.18800 (5 reqs/sec) | Rendering: 0.00000 (0%) | DB: 
0.12400 (65%) | 201 Created [http://localhost/simulations.xml]

The examples that I have found in my research either deal 
with "sibling" elements – such as projects, tasks and locations from 
_Flexible Rails_,  or else elements that are created at different 
times and aren't tied together, such as a brokerage account, stock 
positions and movements from _Flex On Rails_.  I haven't found any 
examples where the models are so tightly bound together where one 
element can't exist without at least one each of the other models.  

Any suggestions would be greatly appreciated. 


Reply via email to