【问题标题】:Rails 3.2.8 Record `save' method execute without errors,but didn't change the databaseRails 3.2.8 Record `save' 方法执行没有错误,但没有更改数据库
【发布时间】:2013-01-07 13:00:57
【问题描述】:

这是 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 istime',但我认为它应该是`datetime'

我的迁移是: 类 AddExpireTimeToUsers

【问题讨论】:

  • 如果你调用u.save!,你会得到一个例外吗?如果是这样,您能否将该错误信息添加到您的问题中。
  • 你是在使用rails console命令加载rails环境吗?如果是,您的用户模型中是否存在限制更新访问权限的 expire_time 字段的任何约束。另外,尝试更新任何其他字段以检查它是否已更新。

标签: ruby-on-rails-3


【解决方案1】:

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

【讨论】:

  • 是的,我刚刚发现我把datetime拼错成了datatime。非常想你们
猜你喜欢
  • 2020-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-29
  • 1970-01-01
  • 2020-12-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多