Reputation: 19506
Recently I realized that none of my database columns have default values. If an object is instantiated and then saved, it might have nil
values for any fields that aren't filled out.
I can make it so that I explicitly initialize these values, but then I've got some places where two controllers are creating the same object, but I didn't think of moving that code into a separate module.
I can also choose to update my migrations to specify default values, which seems cleaner.
I've decided to go with migrations. It is not a good idea to edit old migrations, so I'm creating a new migration and specifying that I want to change certain columns.
Looking at how I should change columns, I have determined that I will use this
def change
change_column :products, :size, :default => 0
end
Will this modify existing records that currently have nil
set for those records?
Upvotes: 0
Views: 2400
Reputation: 1555
No, it will not update your old records.
You should NEVER change old migrations, it is a relief you noticed that.
I suggest you to create a rake task that will update all the fields OR you can do it directly on console like the code below.
Product.update_all({ size: 0 }, { size: nil })
Upvotes: 2