得到的ActiveRecord :: AssociationTypeMismatch型号保存 [英] Got ActiveRecord::AssociationTypeMismatch on model save
本文介绍了得到的ActiveRecord :: AssociationTypeMismatch型号保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对储蓄模型与我的REST API的一个问题。我有许多任务和卡模型中的一个相关联的客户:
I got a problem on saving model with my rest API. I have a Card model with many tasks and one Customer associated :
class Card < ActiveRecord::Base
belongs_to :customer
has_many :card_tasks
has_many :tasks, :through => :card_tasks
accepts_nested_attributes_for :tasks
accepts_nested_attributes_for :card_tasks
accepts_nested_attributes_for :customer
end
class CardTask < ActiveRecord::Base
belongs_to :task
belongs_to :card
accepts_nested_attributes_for :task
accepts_nested_attributes_for :card
end
class Task < ActiveRecord::Base
has_many :cards, :through => :card_tasks
has_many :card_tasks
end
当我送一个JSON是这样的:
When i send a json like this :
{
"card" = > {
"miscellaneous" = > "Obervations diverses",
"heater" = > "0",
"water_quality" = > "",
"customer" = > {
"id" = > "2", "house_name" = > "house_name2", "city" = > "city_2", "lastname" = > "lastname2", "sci" = > "sci2", "postal_code" = > "potal_code_2", "address_line_1" = > "address_line_2", "updated_at" = > "2012-03-05 18:20:57 +0000", "created_at" = > "2012-03-05 18:20:54 +0000", "firstname" = > "firstname2", "address_line_2" = > "address_line_3", "water_used" = > "0"
},
"tasks" = > [
{
"title" = > "Nettoyage ligne eau", "id" = > "6", "updated_at" = > "2012-02-17 08:40:47 +0000", "created_at" = > "2012-02-17 08:40:47 +0000"
},
{
"title" = > "Surveillance", "id" = > "4", "updated_at" = > "2012-02-17 08:40:47 +0000", "created_at" = > "2012-02-17 08:40:47 +0000"
}
]
}
}
我的创建操作:
My create action :
def create
card = Card.new(params[:card])
if (card.save)
respond_with({ :card => card} , :location => nil, status: :created) and return
end
respond_with({ :errors => card.errors }, :location => nil, status: :unprocessable_entity) and return
end
在这样做,我得到了:
ActiveRecord::AssociationTypeMismatch (Task(#70249431354580) expected, got ActiveSupport::HashWithIndifferentAccess(#70249421573300)):
app/controllers/cards_controller.rb:14:in `new'
app/controllers/cards_controller.rb:14:in `create'
我做了什么错了?
What did i do wrong?
推荐答案
现在的问题是在JSON结构,导轨预计 tasks_attributes
,不是任务
。检查<一href="http://stackoverflow.com/questions/8715682/backbone-and-rails-associations-avoiding-json-hashwithindifferentaccess-errors">this问题获取详细信息。
The problem is in the JSON structure, Rails expects tasks_attributes
, not tasks
. Check this question for details.
这篇关于得到的ActiveRecord :: AssociationTypeMismatch型号保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文