Add timestamps to an existing table

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

    I made a simple function that you can call to add to each table (assuming you have a existing database) the created_at and updated_at fields:

      # add created_at and updated_at to each table found.
      def add_datetime
        tables = ActiveRecord::Base.connection.tables
        tables.each do |t|
          ActiveRecord::Base.connection.add_timestamps t  
        end    
      end
    

提交回复
热议问题