This project is archived and is in readonly mode.

#2413 ✓invalid
Joshua White

nested_attributes_for

Reported by Joshua White | April 3rd, 2009 @ 10:52 PM | in 2.x

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

Comments and changes to this ticket

  • Joshua White

    Joshua White April 5th, 2009 @ 05:08 AM

    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.

  • Eloy Duran

    Eloy Duran July 11th, 2009 @ 12:39 PM

    • 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

  • af001

    af001 May 5th, 2011 @ 03:00 AM

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

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Pages