Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails migration: add same column to multiple tables

Is there some special syntax to add timestamp column to multiple tables?

like image 391
Maddy.Shik Avatar asked Apr 01 '11 02:04

Maddy.Shik


1 Answers

Not a specific special-purpose syntax, but you can certainly iterate over an array of table names, and perform the same migration steps on each one.

class AddTimeStampsToABandC < ActiveRecord::Migration
  AFFECTED_TABLES = [:table_a, :table_b, :table_c]

  def self.up
    AFFECTED_TABLES.each do |t|
      add_timestamps(t)
    end
  end

  def self.down
    AFFECTED_TABLES.each do |t|
      remove_timestamps(t)
    end
  end
end
like image 130
Steve Jorgensen Avatar answered Nov 03 '22 08:11

Steve Jorgensen