[英]Rails active record not saving as expected
我有一个更新方法,它没有按预期方式保存数据库记录。
基本上,正在发生的是排列_id没有保存到时隙表中,正如我期望的那样,使用以下方法。
从我变量的输出之后save!
的我会认为,Transactions_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)
首先,奇怪的是您尝试连续保存两次相同的记录。 只要这些记录的ID相同,第二个保存就应该覆盖第一个。
另一方面,由于Rails 2.0左右,Rails具有“脏”对象和属性的概念。 所以很可能是分配
new_timeslot.arrangement_id = @arrangement.id
如果arrangement_id
属性已经具有@arrangement.id
的值,则实际上不会更改任何内容。 因此, new_timeslot
对象不会被标记为“脏”,并且rails可以跳过save!
作为一种优化措施。 即使您预先更新了数据库中的同new_timeslot
记录, new_timeslot
仍然不会被标记为“脏”,因为您是在不同的实例中进行的。
因此,或者您要保存两个不同的记录,然后应该将至少一个的id更改为nil
(或创建一个新记录并使用相同的属性对其进行初始化),或者只想使用new_timeslot
属性来更新记录,然后没有理由致电save!
在old_timeslot
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.