如何指定所有表应包含某些字段?

Cal*_*lin 4 ruby-on-rails rails-migrations

我已经用很多表(大约40个)定义了我的数据库.我现在意识到我想在每个表中添加某些列.为了这个例子,让它成为 created_byupdated_by.

有没有办法在不进行40次迁移的情况下轻松完成这些操作并手动更新每个迁移?

我正在使用rails 2.3.8

Dyl*_*kow 12

您可以生成单个迁移并将此代码放入其中.它将为您提供所有表的数组(减去Rails自动创建的"schema_migrations"表),然后将列添加到每个表中.

tables = ActiveRecord::Base.connection.tables - ["schema_migrations"]
tables.each do |table|
  add_column table, :created_by, :integer
end
Run Code Online (Sandbox Code Playgroud)