Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!!!

like image 464
mandar.gokhale Avatar asked Sep 09 '25 16:09

mandar.gokhale


1 Answers

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
like image 196
Frederick Cheung Avatar answered Sep 13 '25 00:09

Frederick Cheung