【发布时间】:2013-03-08 00:20:37
【问题描述】:
我正在编写迁移以向表中添加列。该列的值取决于另外两个现有列的值。最好/最快的方法是什么? 目前我有这个但不确定这是否是最好的方法,因为组表可能非常大。
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
【问题讨论】:
-
为什么向下反转了向上没有做的事情?
-
这只是我在这里编辑时打错了;)
标签: ruby-on-rails activerecord migration rails-activerecord rails-migrations