Rails 3.2.8记录“保存”方法执行没有错误,但没有更改数据库

时间:2013-01-07 13:00:58

标签: ruby-on-rails-3

以下是rails console中的输出:

Loading development environment (Rails 3.2.8)
irb(main):001:0> u=User.first
  User Load (0.2ms)  SELECT "users".* FROM "users" LIMIT 1
=> #<User id: 1, name: "scorpio_et", passwd: "xxx", updated_at: "2013-01-07 12:09:26",expire_time: "2000-01-01 12:09:15">
irb(main):002:0> u.expire_time=Time.now
=> 2013-01-07 20:16:39 +0800

然后我执行u.save,从输出它似乎工作

irb(main):003:0> u.save
  (0.1ms)  begin transaction
  (0.5ms)  UPDATE "users" SET "expire_time" = '2013-01-07  12:16:39.628766', "updated_at" = '2013-01-07 12:16:42.816250' WHERE "users"."id" = 1
  (85.9ms)  commit transaction
=> true

但是当我从数据库中取出时,`expire_time'字段没有改变

irb(main):004:0> User.first
  User Load (0.4ms)  SELECT "users".* FROM "users" LIMIT 1
=> #<User id: 1, name: "scorpio_et", passwd: "xxx",  updated_at: "2013-01-07 12:16:42", expire_time: "2000-01-01 12:16:39">

当我使用save!' ,it didn't throw exception,and the output is identical with the save'时。但我可以更新其他字段。 这是schema.rb:     ActiveRecord :: Schema.define(:version =&gt; 20130107073652)做

  create_table "users", :force => true do |t|
    t.string   "name"
    t.string   "passwd"
    t.string   "jsoncookie"
    t.datetime "created_at",  :null => false
    t.datetime "updated_at",  :null => false
    t.time     "expire_time"
 end

我注意到expire_time' field is时间',但我认为应该是'datetime'

我的迁移是:     class AddExpireTimeToUsers&lt; ActiveRecord的::迁移       改变         add_column:users,:expire_time,:datatime       结束     端

1 个答案:

答案 0 :(得分:3)

记录会更新(比较时间12:09:15 - &gt; 12:16:39)但看起来expire_timetime类型,而不是datetime。因此日期部分不存储在数据库中。