在已经部署的应用程序中,在 my 中seeds.rb,我有以下内容:
State.create!(:state => "Alabama")
State.create!(:state => "Delaware")
...
现在我想为每个州添加两个字母的代码。
所以我做了这样的迁移:
class AddStateCodeToStates < ActiveRecord::Migration
def self.up
add_column :states, :state_code, :string
update(<<-SQL
UPDATE states SET state_code='WA' where state = 'Washington'
SQL
)
...lots of these SQL statement...
end
def self.down
end
end
问题是:
在开发环境中,当我想从头开始重新创建数据库时,迁移运行后,此时seeds.rb尚未运行。
因此,迁移UPDATE xxx中的AddStateCodeToStates没有可使用的数据(states表为空,因为数据将从 中填充seeds.rb),因此state_code剩下NULL.
所以我的问题是(它们是如此相关,很抱歉没有将它们作为每个单独的问题来问):
- 如何
state_codes在重新创建数据库时填充(在states表中有数据之后)? - 如何获取已部署应用程序的时间(
state_codes不运行)rake db:migrateseeds.rbrake db:migrate - 我不应该
seeds.rb首先使用(而是将数据放入迁移中)吗?