我有一個更新方法沒有按預期保存數據庫記錄。Rails活動記錄沒有按預期保存
基本上,發生的情況是,arrange_id沒有保存到時隙表中,因爲我期望使用下面的方法。
從save!
之後我的變量的輸出中,我認爲arrange_id會保存在時間表中。
任何人都可以告訴我爲什麼這不是,我在這裏做錯了什麼?
# PUT /arrangements/1
def update
success = false
@arrangement = Arrangement.find(params[:id])
# Remove arrangement id from old timeslot
old_timeslot = Timeslot.find(@arrangement.timeslot_id)
old_timeslot.arrangement_id = nil
# Save arrangement id to new timeslot
new_timeslot = Timeslot.find(@arrangement.timeslot_id)
new_timeslot.arrangement_id = @arrangement.id
p "old_timeslot = #{old_timeslot.inspect}"
p "new_timeslot = #{new_timeslot.inspect}"
old_timeslot.save!
new_timeslot.save!
p "after save old_timeslot = #{old_timeslot.inspect}"
p "after save new_timeslot = #{new_timeslot.inspect}"
@arrangement.update_attributes!(params[:arrangement])
success = true
respond_to do |format|
if success
format.html { redirect_to @arrangement, notice: 'Arrangement was successfully updated.' }
else
format.html { render action: "edit" }
end
end
end
這裏是控制檯輸出:
所有真奇怪,你嘗試做兩個# These are before the old_timeslot and new_timeslot variables are saved
"old_timeslot = #<Timeslot id: 17306, location_id: 3, arrangement_id: nil, timeslot: \"2014-01-10 17:00:00\", created_at: \"2013-05-20 04:03:30\", updated_at: \"2014-01-11 01:35:55\">"
"new_timeslot = #<Timeslot id: 17306, location_id: 3, arrangement_id: 839, timeslot: \"2014-01-10 17:00:00\", created_at: \"2013-05-20 04:03:30\", updated_at: \"2014-01-11 01:35:55\">"
# These are after the old_timeslot and new_timeslot variables are saved
"after save old_timeslot = #<Timeslot id: 17306, location_id: 3, arrangement_id: nil, timeslot: \"2014-01-10 17:00:00\", created_at: \"2013-05-20 04:03:30\", updated_at: \"2014-01-11 01:37:09\">"
"after save new_timeslot = #<Timeslot id: 17306, location_id: 3, arrangement_id: 839, timeslot: \"2014-01-10 17:00:00\", created_at: \"2013-05-20 04:03:30\", updated_at: \"2014-01-11 01:35:55\">"
Started PUT "/arrangements/839" for 127.0.0.1 at 2014-01-10 19:37:09 -0600
Processing by ArrangementsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"REBgw/kRwlclqS670aIcIZ1Ug6kgxr/itEevwMQO2w8=", "arrangement"=>{"family_name"=>"smith", "location_id"=>"3", "date"=>"01/10/2014", "timeslot_id"=>"17306", "need"=>"myNeed", "notes"=>"mynotes", "user_id"=>"66"}, "button"=>"", "id"=>"839"}
User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 LIMIT 1
Setting Load (0.4ms) SELECT `settings`.* FROM `settings`
Arrangement Load (0.4ms) SELECT `arrangements`.* FROM `arrangements` WHERE `arrangements`.`id` = 839 LIMIT 1
Timeslot Load (0.2ms) SELECT `timeslots`.* FROM `timeslots` WHERE `timeslots`.`id` = 17306 LIMIT 1
CACHE (0.0ms) SELECT `timeslots`.* FROM `timeslots` WHERE `timeslots`.`id` = 17306 LIMIT 1
(0.1ms) BEGIN
(0.3ms) UPDATE `timeslots` SET `arrangement_id` = NULL, `updated_at` = '2014-01-11 01:37:09' WHERE `timeslots`.`id` = 17306
(53.5ms) COMMIT
(0.1ms) BEGIN
(0.2ms) COMMIT
Redirected to http://localhost:3000/arrangements/839
Completed 302 Found in 168ms (ActiveRecord: 55.5ms)
Ahhh我沒有意識到你提到的這個「骯髒」的對象。這真的把燈泡翻轉了一下,因爲我只是在更新排列記錄時發生錯誤而不改變時間段。這現在完全有意義。此外,在我的代碼old_timeslot和new_timeslot實際上得到不同的記錄,但在我的問題,它看起來像他們得到相同的B/C我想嘗試的東西..非常感謝。 – Catfish