开发者

Validations for boolean type

开发者 https://www.devze.com 2023-03-15 17:39 出处:网络
Should I write any validations for attributes that have type boolean in my schema? create_table \"table_name\", :force => true do |t|

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?


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.


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 :)

0

精彩评论

暂无评论...
验证码 换一张
取 消