I am trying to do something that I feel should be very simple. I have four models as follows:
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
I want to have one form where end users can create both students and checkedout_items in one form. So, I have a view that creates the form and produces the following hash structure:
{"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"}
When this data is posted to my controller, I attempt to create a new checkout object as follows:
@checkout = Checkout.new(params[:checkout])
However, this causes the error: "Couldn't find Item with ID=1 for CheckedoutItem with ID=". I have tried to search for other problems related to this, but I didn't find much.
One final key piece of information is that an Item should always exist before a checkout is created so the items with ID 1 and 2 already exist in the database. Can anyone provide some insight as to what is going wrong?
EDIT: Controller method:
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
Error:
Couldn't find Item with ID=2 for CheckedoutItem with ID=
Partial trace:
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 version: 3.2.16