1 year ago

#316469

test-img

exvayn

Rails migration: Adding a new column with different default values for existing and new records

I have table with a lot of records. I want to add new not null column with default value: false. But for existing records, I want to have true as the value for the new column.

What is the most correct way to do this?

I have an idea to add a not null column with a default value of true. It will set the value to true for all existing records. am I right?

And after that I will change the default value to false for all future entries:

add_column :my_table, :new_field, :boolean, null: false, default: true
change_column_default :my_table, :new_field, from: true, to: false

Or there is something under the hood?

ruby-on-rails

rails-migrations

0 Answers

Your Answer

Accepted video resources