无法更新Rails中的属性值

时间:2015-07-08 14:59:18

标签: ruby-on-rails-4 rails-activerecord

Key模型具有以下迁移

class CreateKeys < ActiveRecord::Migration
  def change
    create_table :keys do |t|
      t.string :value
      t.integer :alive_time
      t.integer :block_time

      t.timestamps null: false
    end
  end
end

运行以下内容不会更新block_time值:

@available_keys = Key.where(:block_time => 0)
@key = Key.find(@available_keys[0].id)
@key.update_attribute(:block_time, Time.now.to_i)

我尝试了rails console中的一些变体:

正在运行: Key.find(10).update_attributes({:block_time => Time.now.to_i})
原因: UPDATE "keys" SET "value" = ?, "alive_time" = ?, "updated_at" = ? WHERE "keys"."id" = ?

正在运行的SQL会更新alive_time,但不会更新block_time

为什么呢?我如何确保正确的行为?

修改
schema.rb内容:

ActiveRecord::Schema.define(version: 20150708123716) do

  create_table "keys", force: :cascade do |t|
    t.string   "value"
    t.integer  "alive_time"
    t.integer  "block_time"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

end

0 个答案:

没有答案