I'd like to add a column to my table with a default value of FALSE, but for current data entries the value should be TRUE.
For example:
class AddDidFoo < ActiveRecord::Migration[6.1]
def change
add_column :mytable, :didFoo, :bool, default: false
end
end
This code will add the didFoo
column with false values to all current data rows and "false" for every new row. I'd like to set all existing rows to "true" in the migration process.
CodePudding user response:
You can do it by running an update_all
just after adding the new column:
# add new column with default value
add_column :mytable, :didFoo, :bool, default: false
# make all current records to have a different value
Mytable.update_all(didFoo: true)