HashWithIndifferentAccesswhile in post request for many to one relationship

50 Views Asked by At
class Item < ActiveRecord::Base
  attr_accessible :name, :item_reviews
  has_many :item_reviews, :dependent => :destroy
  accepts_nested_attributes_for :item_reviews
end

class ItemReview < ActiveRecord::Base
  attr_accessible :price, :value_for_money
  belongs_to :item
end

I am using a multi-model form for post request of a new item (with review). I am using following parameters in post request:

{"utf8"=>"✓",
 "authenticity_token"=>"oZZ6T5bxWHnSiO2Tdz3eFUVCrRH3lzzxdBpuJjlWcho=",
 "item"=>{"name"=>"test",
 "item_reviews"=>[{"value_for_money"=>"1",
 "price"=>"25000"}]},
 "commit"=>"Submit"}

But I got following error while saving:

ItemReview(#89032230) expected, got ActiveSupport::HashWithIndifferentAccess(#77848120)

I suspect array in item_reviews as the culprit, so I did the following:

params[:item][:item_reviews] =  params[:item][:item_reviews][0]

but then I started getting following error:

ItemReview(#87446700) expected, got Array(#75744590)

How can I solve it?

0

There are 0 best solutions below