Can I pass default value to rails generate migration?

一世执手 提交于 2019-11-30 08:09:48

You can't: https://guides.rubyonrails.org/active_record_migrations.html#column-modifiers

null and default cannot be specified via command line.

Rails migration generator does not handle default values, but after generation of migration file you should update migration file with following code

add_column :users, :disabled, :boolean, default: false

you can also see this link - http://api.rubyonrails.org/classes/ActiveRecord/Migration.html

Default migration generator in Rails does not handle default values, there is no way around as of now to specify default value defined through terminal in rails migration.

you would like to follow below steps in order to achieve what you want

1). Execute

$ rails generate migration add_disabled_to_users disabled:boolean

2). Set the new column value to TRUE/FALSE by editing the new migration file created.

class AddDisabledToUsers < ActiveRecord::Migration
  def change
    add_column :users, :disabled, :boolean, default: false
  end
end

3). Run above generated migration by Executing.

$ rake db:migrate

Rails 3.2 does not seem to support any kind of command line type modifier based on http://guides.rubyonrails.org/v3.2/migrations.html

The documentation for Rails 4.1 refers to type modifiers but the documentation for Rails 3.2 does not mention the word "modifier" in the page.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!