Rails set default DateTime to now

前端 未结 5 1101
Happy的楠姐
Happy的楠姐 2021-01-06 00:42

In my app I have teams and each team has a game time every week. I want the game times to be set to \'now\' as a default. My table is set up like so

create_         


        
5条回答
  •  广开言路
    2021-01-06 01:16

    The way I found, was to do a migration on an existing datetime column, like this:

    #migration
    execute("ALTER TABLE teams ALTER COLUMN wk1_time SET DEFAULT CURRENT_TIMESTAMP")
    

    that produces a schema.rb entry shown like this:

    #schema.rb
    t.datetime "wk1_time",                    default: "now()", null: false
    

    The "now()" is a string sent to postgresql and evaluated at runtime, upon each insert

提交回复
热议问题