Saya mencoba melakukan sesuatu yang menurut saya seharusnya sangat sederhana. Saya memiliki empat model sebagai berikut:
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
Saya ingin memiliki satu formulir di mana pengguna akhir dapat membuat siswa dan item_checkout dalam satu formulir. Jadi, saya memiliki tampilan yang membuat formulir dan menghasilkan struktur hash berikut:
{"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"}
Ketika data ini diposting ke pengontrol saya, saya mencoba membuat objek checkout baru sebagai berikut:
@checkout = Checkout.new(params[:checkout])
Namun, hal ini menyebabkan kesalahan: "Tidak dapat menemukan Item dengan ID=1 untuk CheckedoutItem dengan ID=". Saya telah mencoba mencari masalah lain yang terkait dengan ini, tetapi saya tidak menemukan banyak.
Salah satu informasi penting terakhir adalah bahwa Item harus selalu ada sebelum checkout dibuat sehingga item dengan ID 1 dan 2 sudah ada di database. Adakah yang bisa memberikan wawasan tentang apa yang salah?
EDIT: Metode pengontrol:
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
Kesalahan:
Couldn't find Item with ID=2 for CheckedoutItem with ID=
Jejak sebagian:
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'
Versi rel: 3.2.16