我的理解是你要更新用戶記錄,而不是創建種子(種子添加數據到你的數據庫,例如角色等等,這對你的應用程序很重要)。從documentaion:
class AddReceiveNewsletterToUsers < ActiveRecord::Migration
def self.up
change_table :users do |t|
t.boolean :receive_newsletter, :default => false
end
User.update_all ["receive_newsletter = ?", true]
end
def self.down
remove_column :users, :receive_newsletter
end
end
你也可以用你的模型內遷移:
User.all(:conditions => {}).each do |user|
user.do_sth
user.save
end
等,閱讀about caveats
編輯:
您的評論後,這是我認爲你應該在移民中做到這一點;
class AddInvitationLimitToUser < ActiveRecord::Migration
def selt.up
change_table :users do |t|
t.integer :invitation_limit # and add validation to model `validates_presence_of` and so on
end
# now update all records already in database, which didn't had value in invitation_limit column
User.update_all ["invitation_limit = ?", 5]
end
def self.down
remove_column, :users, :invitation_limit
end
end
來源
2010-01-25 07:51:54
MBO
這就是我正在尋找的,謝謝! – 2010-01-25 07:19:53