Ruby on rails 3 如何在创建迁移时将默认值设置为rails中的列
我是rails的新模特。我知道如何创建模型以及如何向模型中添加列。现在,我想将默认值设置为一列,但我不知道该怎么做 我生成了新模型Ruby on rails 3 如何在创建迁移时将默认值设置为rails中的列,ruby-on-rails-3,activerecord,rails-migrations,Ruby On Rails 3,Activerecord,Rails Migrations,我是rails的新模特。我知道如何创建模型以及如何向模型中添加列。现在,我想将默认值设置为一列,但我不知道该怎么做 我生成了新模型 rails g model User 然后在其中添加了列 rails generate migration AddNotificationEmailToUsers notification_email:boolean 现在我想将通知列default的值设置为true。 请指导我如何编写相同的迁移。谢谢你 您不能从命令行执行此操作-您必须编辑迁移文件并将相应的行更
rails g model User
然后在其中添加了列
rails generate migration AddNotificationEmailToUsers notification_email:boolean
现在我想将通知列default的值设置为true。
请指导我如何编写相同的迁移。谢谢你 您不能从命令行执行此操作-您必须编辑迁移文件并将相应的行更改为
add_column :users, :notification_email, :boolean, :default => true
Frederick Cheung是正确的,您需要为此编辑迁移文件。 只需进行一次小的更新,在指定默认值之前,在数据类型之后添加逗号
add_column :users, :notification_email, :boolean, :default => true
这里最好的方法是在迁移中使用
change\u列
。它被播发为更改类型,但您可以使用它将默认值附加到现有列
我有
location :integer
在schema中,我希望默认为零,因此我编写了如下迁移:
change_column :player_states, :location, :integer, :default => 0
这就成功了。到目前为止,还没有办法在rails迁移中指定通过terminal定义的默认值 您可以执行以下步骤来指定列的默认值 1) 。执行
$ rails generate migration AddNotificationEmailToUsers notification_email:boolean
2) 。通过编辑创建的新迁移文件,将新列默认值指定为TRUE/FALSE
class AddNotificationEmailToUsers < ActiveRecord::Migration
def change
add_column :users, :notification_email, :boolean, default: true
end
end
嘿,伙计们,这对我不起作用。有没有像rails g migration AddDefaultsToTablename:Tablename,:ColumnName:Default value这样的迁移?没有,没有直接的方法,这应该在生成的迁移文件中起作用,你得到了什么错误?谢谢,这帮助了我
$ rake db:migrate