Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

nested_attributes_for

#2413

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

Reported by Joshua White · April 3rd, 2009 @ 10:52 PM

State: invalid
Milestone: 2.x
Assigned to: Eloy Duran Eloy Duran
Importance: none

Activity

  1. Joshua White
    Joshua White

    I figured out the problem

    In my hash below

    "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"}], }}

    I needed the hash key for the finding_measurement_datas to be called finding_measurement_datas_attributes

    If I were using rails views and not flex xml I wouldn't have had the problem so its not a bug, just that the params converstion is not well documented.

    April 5th, 2009 @ 05:08 AM

  2. Eloy Duran
    Eloy Duran
    • State changed from new to invalid

    Joshua, if you find the documentation on this lacking, please take a look at extending the docs through the doc-rails project: http://github.com/lifo/docrails/tree/master

    Thanks

    July 11th, 2009 @ 12:39 PM

  3. af001
    af001

    私の中で、総合評価のとっても低いアバアバクロホリスタークロ銀座店。アバクロは大好きなんですけどね。一昨日の東京駅付近での打ち合わせの後、散歩がてら久々に行ってきました。そしたらビックリ!相変わらアバクロず、踊っているだけの店員さんとかもいましたが、

    May 5th, 2011 @ 03:00 AM