Rails活动记录未按预期保存

时间:2014-01-11 01:49:06

标签: ruby-on-rails ruby-on-rails-3

我有一个不按预期保存数据库记录的更新方法。

基本上发生的事情是,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)

1 个答案:

答案 0 :(得分:1)

首先,您尝试连续两次保存同一记录是很奇怪的。只要这些记录的id相同,第二次保存就应该覆盖第一次。

另一方面,由于Rails 2.0左右Rails具有“脏”对象和属性的概念。所以很可能是作业

new_timeslot.arrangement_id = @arrangement.id
如果arrangement_id属性已经具有值@arrangement.id,则

实际上不会更改任何内容。因此,new_timeslot对象不会被标记为“脏”,并且rails可以跳过save!作为优化措施。即使您事先在数据库中更新了相同的记录,new_timeslot仍然没有被标记为'脏',因为您在另一个实例中执行了此操作。

因此,要么您要保存两个不同的记录,那么您应该将至少一个的id更改为nil(或创建一个新记录并使用相同的属性对其进行初始化)或者您只想更新使用new_timeslot属性进行记录,然后没有理由在save!上致电old_timeslot