我正在寫一個移動到列添加到表。該列的值取決於另外兩個現有列的值。什麼是最好的/最快的方式來做到這一點? 目前我有這個,但不知道這是否是最好的方式,因爲groups表可能非常大。添加一個數據庫列與Rails的遷移,並填充它基於另一列
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
爲什麼倒下來的東西不起作用? – Robert 2013-03-08 01:17:01
這只是我在編輯時犯的一個錯字;) – user1404536 2013-03-08 01:21:01