everydayrails-rspec-2017 icon indicating copy to clipboard operation
everydayrails-rspec-2017 copied to clipboard

Chapter 11 null value in boolean column

Open JunichiIto opened this issue 7 years ago • 0 comments

Chapter 11, page 192,

NOTE: This is a problem related to RDBMS.

bin/rails g migration add_completed_to_projects completed:boolean
bin/rails db:migrate

This migration fills null value in existing projects. And developers have to distinguish null or false, like this:

# If there are ten projects before running the migration 
Project.where(completed: false).count #=> 0
Project.where(completed: null).count #=> 10

This issue could lead to a bug because developers tend to believe that completed column has true or false only. So I think it is better to edit the migration file like this:

class AddCompletedToProjects < ActiveRecord::Migration[5.1]
  def change
    add_column :projects, :completed, :boolean, default: false, null: false
  end
end

JunichiIto avatar Feb 11 '18 22:02 JunichiIto