I’ve seen a few questions (namely this one) here on SO about adding a default boolean value to an existing column. So I tried the change_column suggestion but I mustn’t be doing it right.
I tried:
$ change_column :profiles, :show_attribute, :boolean, :default => true
Which returns -bash: change_column: command not found
I then ran:
$ rails g change_column :profiles, :show_attribute, :boolean, :default => true
…and
$ rails change_column :profiles, :show_attribute, :boolean, :default => true
Then ran rake db:migrate, but the value for :show_attribute remained nil. In the question I referenced above it says in PostgreSQL you need to update it manually. Since I’m using PostgreSQL I added the following in my create_profiles migration:
t.boolean :show_attribute, :default => true
Can someone tell me what I’m doing wrong here?
change_columnis a method ofActiveRecord::Migration, so you can’t call it like that in the console.If you want to add a default value for this column, create a new migration:
rails g migration add_default_value_to_show_attributeThen in the migration created:
OR a more specific option:
Then run
rake db:migrate.It won’t change anything to the already created records. To do that you would have to create a
rake taskor just go in therails consoleand update all the records (which I would not recommend in production).When you added
t.boolean :show_attribute, :default => trueto thecreate_profilesmigration, it’s expected that it didn’t do anything. Only migrations that have not already been ran are executed. If you started with a fresh database, then it would set the default to true.