Add timestamps to an existing table

前端 未结 21 1140
予麋鹿
予麋鹿 2020-12-12 14:34

I need to add timestamps (created_at & updated_at) to an existing table. I tried the following code but it didn\'t work.

class          


        
21条回答
  •  情话喂你
    2020-12-12 15:12

    Using Time.current is a good style https://github.com/rubocop-hq/rails-style-guide#timenow

    def change
      change_table :users do |t|
        t.timestamps default: Time.current
        t.change_default :created_at, from: Time.current, to: nil
        t.change_default :updated_at, from: Time.current, to: nil
      end
    end
    

    or

    def change
      add_timestamps :users, default: Time.current
      change_column_default :users, :created_at, from: Time.current, to: nil
      change_column_default :users, :updated_at, from: Time.current, to: nil
    end
    

提交回复
热议问题