我正在尝试做一些我认为应该非常简单的事情。我有四个模型如下:
class Checkout < ActiveRecord::Base
has_many :checkedout_items
belongs_to :student, :autosave => true
attr_accessible :student_id, :status, :checkedout_items_attributes, :student_attributes, :status
accepts_nested_attributes_for :checkedout_items
accepts_nested_attributes_for :student
end
class CheckedoutItem < ActiveRecord::Base
belongs_to :checkout, :autosave => true
has_one :item, :foreign_key => "id"
attr_accessible :enddate, :checkout_id, :item_id, :startdate, :status, :item_attributes
accepts_nested_attributes_for :item
end
class Item < ActiveRecord::Base
attr_accessible :name, :category
end
class Student < ActiveRecord::Base
has_many :checkouts, dependent: :destroy
attr_accessible :email, :firstname, :lastname, :phonenumber, :uin
end
我希望有一个表单,最终用户可以在一个表单中创建学生和checkedout_items。所以,我有一个创建表单并生成以下哈希结构的视图:
{"utf8"=>"✓",
"authenticity_token"=>"m6yH1LhtOk/kDqpLDRlNkxFSAA1WmGARywgT4DwYmKo=",
"checkout"=> {
"student_attributes"=> {
"firstname"=>"Jimmy",
"lastname"=>"Johnson",
"uin"=>"899006555",
"email"=>"jj@test.com",
"phonenumber"=>"1234445555"
},
"checkedout_items_attributes"=> {
"0"=> {
"item_attributes"=> {
"id"=>"1",
"name"=>"Camera #1"
},
"startdate(2i)"=>"12",
"startdate(3i)"=>"8",
"startdate(1i)"=>"2014",
"startdate(4i)"=>"04",
"startdate(5i)"=>"00"
},
"1"=> {
"item_attributes"=> {
"id"=>"2",
"name"=>"Camera #2"
},
"startdate(2i)"=>"12",
"startdate(3i)"=>"8",
"startdate(1i)"=>"2014",
"startdate(4i)"=>"04",
"startdate(5i)"=>"00"
}
}
},
"commit"=>"Finish"}
当这些数据发布到我的控制器时,我尝试按如下方式创建一个新的结帐对象:
@checkout = Checkout.new(params[:checkout])
但是,这会导致错误:“对于ID =”的CheckedoutItem,找不到ID = 1的项目。我试图寻找与此相关的其他问题,但我找不到太多。
最后一个关键信息是,在创建结账之前,项应始终存在,因此数据库中已存在ID为1和2的项。谁能提供一些关于出了什么问题的见解?
编辑: 控制器方法:
def create
@checkout = Checkout.new(params[:checkout])
respond_to do |format|
if @checkout.save
format.html { redirect_to @checkout, notice: 'Checkout was successfully created.' }
format.json { render json: @checkout, status: :created, location: @checkout }
else
format.html { render action: "new" }
format.json { render json: @checkout.errors, status: :unprocessable_entity }
end
end
end
错误:
Couldn't find Item with ID=2 for CheckedoutItem with ID=
部分追踪:
activerecord (3.2.16) lib/active_record/nested_attributes.rb:487:in `raise_nested_attributes_record_not_found'
activerecord (3.2.16) lib/active_record/nested_attributes.rb:357:in `assign_nested_attributes_for_one_to_one_association'
activerecord (3.2.16) lib/active_record/nested_attributes.rb:313:in `item_attributes='
activerecord (3.2.16) lib/active_record/attribute_assignment.rb:94:in `block in assign_attributes'
activerecord (3.2.16) lib/active_record/attribute_assignment.rb:93:in `each'
activerecord (3.2.16) lib/active_record/attribute_assignment.rb:93:in `assign_attributes'
activerecord (3.2.16) lib/active_record/base.rb:498:in `initialize'
activerecord (3.2.16) lib/active_record/reflection.rb:183:in `new'
activerecord (3.2.16) lib/active_record/reflection.rb:183:in `build_association'
activerecord (3.2.16) lib/active_record/associations/association.rb:239:in `build_record'
activerecord (3.2.16) lib/active_record/associations/collection_association.rb:112:in `build'
activerecord (3.2.16) lib/active_record/nested_attributes.rb:430:in `block in assign_nested_attributes_for_collection_association'
activerecord (3.2.16) lib/active_record/nested_attributes.rb:425:in `each'
activerecord (3.2.16) lib/active_record/nested_attributes.rb:425:in `assign_nested_attributes_for_collection_association'
activerecord (3.2.16) lib/active_record/nested_attributes.rb:313:in `checkedout_items_attributes='
activerecord (3.2.16) lib/active_record/attribute_assignment.rb:94:in `block in assign_attributes'
activerecord (3.2.16) lib/active_record/attribute_assignment.rb:93:in `each'
activerecord (3.2.16) lib/active_record/attribute_assignment.rb:93:in `assign_attributes'
activerecord (3.2.16) lib/active_record/base.rb:498:in `initialize'
Rails版本:3.2.16