ActiveRecord boolean validation accepting non-boolean values

邮差的信 提交于 2019-12-12 15:44:27

问题


I'm trying to validate that an attribute is a boolean, i.e. true or false.

From the Rails guide I expected

validates :new_out_of_stock, inclusion: { in: [true, false] }

to work, but it accepts non-boolean values (e.g. "Hi") as valid:

irb> ip = ItemPrice.new
irb> ip.new_out_of_stock = "Hi"
=> "Hi"
irb> ip.valid?
=> true
irb> ip.errors.messages
=> {}

It correctly rejects nil and accepts true and false.

All the documentation on the Internet that I've read says that this is the correct way to validate a boolean attribute. Why doesn't it work?

Edit: Here's the class definition:

class ItemPrice < ActiveRecord::Base
    belongs_to :item, counter_cache: true

    validates :new_out_of_stock, inclusion: { in: [true, false] }
end

回答1:


I think that non-boolean values are coerced to booleans when they're assigned to the attribute, causing them to pass the validation.

See:

irb> ip.new_out_of_stock
=> nil
irb> ip.new_out_of_stock = "hi"
=> "hi"
irb> ip.new_out_of_stock 
=> false

and clearly false is a valid boolean value, so the field passes.




回答2:


It's not accept non-boolean values (e.g. "Hi") as valid, you just need to do this:

irb> ip.new_out_of_stock="Hi"
=> "Hi"
irb> ip.valid?
=> false # so it's not accept "Hi"
irb> ip.errors.full_messages
=> ["New_out_of_stock is not included in the list"]
irb> ip.errors.messages
=> {:new_out_of_stock=>["is not included in the list"]}
irb> ip.errors.messages[:new_out_of_stock]
=> ["is not included in the list"]

UPDATE:

Try this in your model:

validate :require_boolean

def require_boolean
  errors.add(:new_out_of_stock, "is not included in the list") unless self.new_out_of_stock.in?([true, false])
end



回答3:


Try to change the syntax slightly to the following

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

This has worked for me in the past



来源:https://stackoverflow.com/questions/20657344/activerecord-boolean-validation-accepting-non-boolean-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!