There is a lot more going on with my models, but I simplified
the class for this bug. I have a class, finding_data, which has
many finding_measurement_datas. Everything works except for
updating with the nested attributes. I assigned class
FindingData < ActiveRecord::Base has_many
:finding_measurement_datas, :dependent => :destroy
accepts_nested_attributes_for :finding_measurement_datas end
class FindingMeasurementData < ActiveRecord::Base
belongs_to :finding_measurement
belongs_to :finding_data
validates_presence_of :finding_measurement
end
I send back params that look like this from my application to
the server.
Parameters: {"id"=>"97",
"exam_record"=>{"name"=>"exam_record", "finding_datas"=>
[{"name"=>"Mammo Mass 1",
"updated_at"=>"2009-03-27T21:54:24-05:00",
"finding_type_id"=>"15", "selected"=>"1", "id"=>"1",
"finding_measurement_datas"=>
[{"finding_measurement_id"=>"15", "id"=>"1",
"measurement"=>"1.34", finding_data_id"=>"1"},
{"finding_measurement_id"=>"19", "id"=>"2",
"measurement"=>"1", "finding_data_id"=>"1"}], }}}
In the controller I say
params[:exam_record][:finding_datas].each do |fd|
if FindingData.exists?(fd[:id])
finding_data = FindingData.find(fd[:id]).update_attributes(fd)
else
finding_data = FindingData.create(fd)
end
end
This should then take the list of finding_measurement_datas that
is in fd and update the finding data, and add any associated
measurements correct? Instead it tells me in the log
ActiveRecord::AssociationTypeMismatch
(FindingMeasurementData(#-608683668) expected, got
HashWithIndifferentAccess(#-605880508)):
app/controllers/readings_controller.rb:193:in
update'
I would just cast each measurement data as a new measurement
data, but some aren't new and they already have ids.
I am trying to follow the documents about doing complex views. I
am sending XML back and fourth to my server, but since rails parses
it into params I assume it should work the same way as the params
hash from the rails views. I also know that I can iterate through
the hash and add each finding measurement on its own, the link it
to the finding data. Should what I'm trying to do work, or am I not
looking at the nested_attributes_for correctly?
Thanks for checking it out