I have a ‘run’ object which can hold many ‘layers’ inside of it. Runs accepts nested attributes for layers, but rails fails to make sure the parameters of the nested object are valid before all kinds of bad stuff happens. I’m trying to check if the ‘deposition_source_id’ parameter is empty on any of the layers. How can I loop through the layers?
This line can get the parameters from ONE or any of the layers. “0” specifies the layer.
params[:run][:layers_attributes]["0"][:deposition_source_id]
How can I check every layer? These are the parameters:
=> {"utf8"=>"✓",
"_method"=>"put",
"authenticity_token"=>"T+X6sSda5vV19hpMZEAdf5RWSKPhJrm/q9+NXxTC5G8=",
"run"=>
{"number"=>"31310.0",
"start_time_string"=>"08/08/2012 at 11:08 AM",
"system_id"=>"4",
"technician_id"=>"4",
"duration"=>"",
"base_pressure"=>"",
"platters"=>"Apples",
"overcoats_string"=>"",
"planetary"=>"",
"layers_attributes"=>
{"0"=>
{"deposition_source_id"=>"",
"material_id"=>"60",
"lot_id"=>"118",
"thickness_goal"=>"32",
"measured_thickness"=>"3",
"tooling_factor"=>"",
"sensor_number"=>"",
"xtal_life"=>"",
"release_agent"=>"0",
"_destroy"=>"false",
"id"=>"3401"}}},
"commit"=>"Update Run",
"action"=>"update",
"controller"=>"runs",
"id"=>"2319"}
You could do something like this:
If all you want to do is make sure that deposition_source_id is always empty, I would do it this way: