Add timestamps to an existing table

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

    This seems like a clean solution in Rails 5.0.7 (discovered the change_column_null method):

    def change
      add_timestamps :candidate_offices, default: nil, null: true
      change_column_null(:candidate_offices, :created_at, false, Time.zone.now)
      change_column_null(:candidate_offices, :created_at, false, Time.zone.now)
    end
    

提交回复
热议问题