Rails database defaults and model validation for boolean fields

前端 未结 4 1543
野性不改
野性不改 2020-12-03 16:39

In a Rails model I have an attribute is_subscriber, when I constructed a db migration to add this column to the database I specified the default value to be fal

4条回答
  •  隐瞒了意图╮
    2020-12-03 17:20

    From here

    If you want to validate the presence of a boolean field (where the real values are true and false), you will want to use validates_inclusion_of :field_name, :in => [true, false] This is due to the way Object#blank? handles boolean values. false.blank? # => true

    Or in Rails3 way

    validates :field, :inclusion => {:in => [true, false]}
    

提交回复
热议问题