how to set default value to column in rails while creating migration

mandar.gokhale picture mandar.gokhale · Dec 26, 2011 · Viewed 35.1k times · Source

I am new to Model in rails. I know how to create model & how to add column to them. Now I want to set default value to a column but I am not getting that how exactly I can do it.

I generated new model

rails g model User

then added column to it

rails generate migration AddNotificationEmailToUsers notification_email:boolean

Now I want to set value of Notification column default as true. Please guide me how to write the migration for the same. Thank you!!!

Answer

Frederick Cheung picture Frederick Cheung · Dec 26, 2011

You can't do this from the command line - you'll have to edit the migration file and change the corresponding line to something like

add_column :users, :notification_email, :boolean, :default => true