Add timestamps to an existing table

前端 未结 21 1145
予麋鹿
予麋鹿 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:19

    The timestamp helper is only available in the create_table block. You can add these columns by specifying the column types manually:

    class AddTimestampsToUser < ActiveRecord::Migration
      def change_table
        add_column :users, :created_at, :datetime, null: false
        add_column :users, :updated_at, :datetime, null: false
      end
    end
    

    While this does not have the same terse syntax as the add_timestamps method you have specified above, Rails will still treat these columns as timestamp columns, and update the values normally.

提交回复
热议问题