亂七八糟的軌道控制檯我發現AR模型對象的方法update_attributes做我想要的。
對於實例
new_project = orig_project.amoeba_dup
new_project.update_attributes(project_params)
...將離開聯想完好,但覆蓋new_project
與project_params
這裏是軌道控制檯輸出來證明這一點。
35: def create
36: if params[:copy]
37: orig_project = Project.find params[:copy]
38: @project = orig_project.amoeba_dup
39:
=> 40: binding.pry
41: else
42: @project = Project.new(project_params)
43: end
44:
45: respond_to do |format|
46: if @project.save
47: format.html { redirect_to @project, notice: 'Project was successfully created.' }
48: format.json { render action: 'show', status: :created, location: @project }
49: else
50: format.html { render action: 'new' }
51: format.json { render json: @project.errors, status: :unprocessable_entity }
52: end
53: end
54: end
[1] pry(#<ProjectsController>)> params[:copy]
=> "201"
[2] pry(#<ProjectsController>)> @project
=> #<Project:0x00000005c38e58
id: nil,
name: "Corn Flour Milling with Setup",
scheduled_start_date: Tue, 29 Dec 2015,
estimated_end_date: Wed, 27 Jan 2016,
employees_needed: 2,
incoming_packaging: "Boxes",
final_product_packaging: "Boxes",
sample_instructions: "One 18 oz every pallet.\r\nMilled Corn Flour\r\nLot Number\r\nBag Number\r\nNet Weight",
project_description: "Mill the piss out of this.",
project_type: "Production",
building_id: "5",
paid_status: false,
material_total_weight_lbs: 40000,
shifts: 1.0,
shift_hrs: 8,
rate_lb_hr: 2000,
company_id: 1,
created_at: nil,
updated_at: nil>
[4] pry(#<ProjectsController>)> @project.setups
=> [#<Setup:0x00000005cid: 22, created_at: Thu, 21 Jan 2016 14:33:09 UTC +00:00, updated_at: Thu, 21 Jan 2016 14:33:09 UTC +00:00>]
[5] pry(#<ProjectsController>)> project_params
=> {"name"=>"Corn Flour Milling with Setup",
"scheduled_start_date"=>"2016-03-25", #changed this value
"estimated_end_date"=>"2016-03-30", #changed this value
"employees_needed"=>"2",
"incoming_packaging"=>"Boxes",
"final_product_packaging"=>"Boxes",
"sample_instructions"=>"One 18 oz every pallet.\r\nMilled Corn Flour\r\nLot Number\r\nBag Number\r\nNet Weight",
"project_description"=>"Mill the piss out of this.",
"project_type"=>"Production",
"building_id"=>"5",
"paid_status"=>"false",
"material_total_weight_lbs"=>"40000",
"shifts"=>"1.0",
"shift_hrs"=>"8",
"rate_lb_hr"=>"2000",
"company_id"=>"1"}
[7] pry(#<ProjectsController>)> @project.update_attributes(project_params)
(0.6ms) BEGIN
Company Load (1.3ms) SELECT "companies".* FROM "companies" WHERE "companies"."id" = $1 LIMIT 1 [["id", 1]]
SQL (1.3ms) INSERT INTO "projects" ("name", "scheduled_start_date", "estimated_end_date", "employees_needed", "incoming_packaging", "final_product_packaging", "sample_instructions", "project_description", "project_type", "building_id", "paid_status", "material_total_weight_lbs", "shifts", "shift_hrs", "rate_lb_hr", "company_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18) RETURNING "id" [["name", "Corn Flour Milling with Setup"], ["scheduled_start_date", "2016-03-25"], ["estimated_end_date", "2016-03-30"], ["employees_needed", 2], ["incoming_packaging", "Boxes"], ["final_product_packaging", "Boxes"], ["sample_instructions", "One 18 oz every pallet.\r\nMilled Corn Flour\r\nLot Number\r\nBag Number\r\nNet Weight"], ["project_description", "Mill the piss out of this."], ["project_type", "Production"], ["building_id", "5"], ["paid_status", "f"], ["material_total_weight_lbs", 40000], ["shifts", 1.0], ["shift_hrs", 8], ["rate_lb_hr", 2000], ["company_id", 1], ["created_at", "2016-01-21 20:07:26.508713"], ["updated_at", "2016-01-21 20:07:26.508713"]]
SQL (1.8ms) INSERT INTO "projects_setups" ("setup_id", "project_id") VALUES ($1, $2) [["setup_id", 22], ["project_id", 217]]
(32.1ms) COMMIT
=> true
[8] pry(#<ProjectsController>)>
@ new_project.update_attributes(project_attributes)做我希望的事情嗎? – JerryA