man*_*ale 22 activerecord rails-migrations ruby-on-rails-3
我是模拟导轨的新手.我知道如何创建模型以及如何向其添加列.现在我想将默认值设置为列,但我没有得到我究竟能做到的.
我生成了新模型
rails g model User
Run Code Online (Sandbox Code Playgroud)
然后在其中添加了列
rails generate migration AddNotificationEmailToUsers notification_email:boolean
Run Code Online (Sandbox Code Playgroud)
现在我想将Notification列的值默认设置为true.请指导我如何编写相同的迁移.谢谢!!!
Fre*_*ung 49
您不能从命令行执行此操作 - 您必须编辑迁移文件并将相应的行更改为类似的行
add_column :users, :notification_email, :boolean, :default => true
Run Code Online (Sandbox Code Playgroud)
小智 14
这里最好的方法是change_column在迁移中使用.它被宣传为更改类型,但您可以使用它将默认值附加到现有列.
我有
location :integer
Run Code Online (Sandbox Code Playgroud)
在架构中,我想默认为零,所以我写了一个迁移:
change_column :player_states, :location, :integer, :default => 0
Run Code Online (Sandbox Code Playgroud)
这就是诀窍.