ฉันกำลังพยายามทำสิ่งที่ฉันรู้สึกว่าควรจะเรียบง่ายมาก ฉันมีสี่รุ่นดังนี้:
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
ฉันอยากมีแบบฟอร์มเดียวที่ผู้ใช้ปลายทางสามารถสร้างทั้งนักเรียนและ checkout_items ได้ในแบบฟอร์มเดียว ดังนั้นฉันจึงมีมุมมองที่สร้างแบบฟอร์มและสร้างโครงสร้างแฮชดังต่อไปนี้:
{"utf8"=>"✓",
"authenticity_token"=>"m6yH1LhtOk/kDqpLDRlNkxFSAA1WmGARywgT4DwYmKo=",
"checkout"=> {
"student_attributes"=> {
"firstname"=>"Jimmy",
"lastname"=>"Johnson",
"uin"=>"899006555",
"email"=>"[email protected]",
"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=1 สำหรับ CheckedoutItem ด้วย ID=" ฉันได้พยายามค้นหาปัญหาอื่น ๆ ที่เกี่ยวข้องกับเรื่องนี้แล้ว แต่ไม่พบมากนัก
ข้อมูลสำคัญประการสุดท้ายคือรายการควรมีอยู่เสมอก่อนที่จะสร้างการชำระเงิน เพื่อให้รายการที่มี 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