Avoid the Three-state Boolean Problem

Quick, what’s wrong with this Rails migration?

add_column :users, :admin, :boolean

Yep – it can be null. Your Boolean column is supposed to be only true or false. But now you’re in the madness of three-state Booleans: it can be true, false, or NULL.

Awesome post from the genius guys at Thoughtbot. This is how to do booleans in Rails:

add_column :users, :admin, :boolean, null: false, default: false

Avoid the Three-state Boolean Problem.

Advertisements