Ruby Flare

Your awesome Tagline

4 notes

Validates presence of booleans

A gotcha that often trips me up - you can’t use the validates_presence_of with a boolean attribute in your rails app.

validates_presence_of :paid

You need to use validates_inclusion_of.

validates_inclusion_of :paid, :in => [true,false]

According to the rails api docs, this is due to the way Object#blank? handles boolean values: false.blank? # => true.

(Source: api.rubyonrails.org)

Filed under rails validations

  1. breakbit reblogged this from rubyflare
  2. rubyflare posted this