xsj0jsx
xsj0jsx

Reputation: 169

Rails 3.2.8 Record `save' method execute without errors,but didn't change the database

Here is the output in 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

then i execute u.save , from the output it seems worked

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

But when i fetch from the database , the `expire_time' field didn't change

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">

When I use save!' ,it didn't throw exception,and the output is identical with thesave' .But I can update other field . Here is the schema.rb: ActiveRecord::Schema.define(:version => 20130107073652) do

  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

I notice the expire_time' field istime',but I think it should be `datetime'

my migration is : class AddExpireTimeToUsers < ActiveRecord::Migration def change add_column :users, :expire_time, :datatime end end

Upvotes: 1

Views: 147

Answers (1)

dimuch
dimuch

Reputation: 12818

The record gets updated (compare the time 12:09:15 -> 12:16:39) but looks like the expire_time has time type, not datetime. So date part is not stored in the db.

Upvotes: 3

Related Questions