我正在撰写一个迁移脚本,目的是向表中添加一列。该列的值取决于另外两列的值。请问最佳和最快的方法是什么? 目前我已经有了以下代码,但不确定它是否是最好的方式,因为groups表可能非常大。
class AddColorToGroup < ActiveRecord::Migration
def self.up
add_column :groups, :color, :string
Groups = Group.all.each do |g|
c = "red" if g.is_active && is_live
c = "green" if g.is_active
c = "orange"
g.update_attribute(:type, c)
end
end
def self.down
end
end