Matthew H
Matthew H

Reputation: 5879

ActiveRecord boolean validation accepting non-boolean values

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

Upvotes: 4

Views: 890

Answers (3)

Matthew H
Matthew H

Reputation: 5879

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.

Upvotes: 3

Michael Moulsdale
Michael Moulsdale

Reputation: 1488

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

Upvotes: 0

user2503775
user2503775

Reputation: 4367

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

Upvotes: 1

Related Questions