Emil Ahlbäck
Emil Ahlbäck

Reputation: 6206

Validations for boolean type

Should I write any validations for attributes that have type boolean in my schema?

create_table "table_name", :force => true do |t|
  t.boolean "column", :default => true
end

Setting column to "asdasd" evaluates to false after .save. Need I be concerned about any weird values sent through POST (I'm writing the update method for my controller) could result in anything dangerous?

Upvotes: 2

Views: 600

Answers (2)

Frankie Roberto
Frankie Roberto

Reputation: 1359

It's probably a good idea to validate, as otherwise you might get unexpected results. This should do you:

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

That will also prevent null values. If you want to allow those, set :allow_nil => true.

Upvotes: 1

felix
felix

Reputation: 11542

In general, it is better to validate, especially if that field is a sensitive one.If the field is like a field that enables some recurrent billing check and if some random string is sent, since your default is true,it will become true. It is a good thing to add validations, considering its so easier in Rails :)

Upvotes: 0

Related Questions