1

这是rails控制台中的输出:

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 thesave' 时。但我可以更新其他字段。这是 schema.rb: ActiveRecord::Schema.define(:version => 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时间',但我认为它应该是'日期时间'

我的迁移是: class AddExpireTimeToUsers < ActiveRecord::Migration def change add_column :users, :expire_time, :datatime end end

4

1 回答 1

3

记录被更新(比较时间12:09:15-> 12:16:39)但看起来像expire_timehastime类型,而不是datetime。所以日期部分不存储在数据库中。

于 2013-01-07T13:51:14.783 回答