如果我是你,我会通过创建一个新列,然后从旧列更新所有内容,然后删除旧的列,重命名做到这一点新的一个。我也不会保存布尔值为“true”或“false”(如果你只是改变类型,Rails应该默认给你)......如果我是你,我会为这一列创建一个enum。
首先迁移:
class ChangeFootball < ActiveRecord::Migration
def change
# Add the new column. Use an integer type, so you can set up an Enum in your model
add_column :examples, :football_new, :integer, default: 0
# Set up the new column values for all your existing records:
Example.where(football: true).update_all football_new: 0
Example.where(football: false).update_all football_new: 1
# Now remove the old column:
remove_column :examples, :football
# Finally, rename the new column to the same as the old one
rename_column :examples, :football_new, :football
# Oh, and add an index:
add_index :examples, :football
end
end
现在设置的枚举在你的模型:
enum football: { own: 0, lost: 1, misplaced: 2, loaned: 3 }
所以,TRUE;将变更为 “真” 和'FALSE'为 “假”? –
是的...... –
如果您意识到您的实时数据,那么在运行实时数据迁移之前,先在rails中创建小应用程序并测试一些虚拟数据。 ;) –