everydayrails / everydayrails-rspec-2017

Sample source for the 2017 edition of Everyday Rails Testing with RSpec.
http://rspectutorial.com
312 stars 247 forks source link

Chapter 11 null value in boolean column #58

Open JunichiIto opened 6 years ago

JunichiIto commented 6 years ago

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