# Problem validating boolean

**URL:** <https://www.ruby-forum.com/t/problem-validating-boolean/103072>\
**Category:** Rails\
**Created:** [May 1, 2007, 2:00pm UTC](https://www.ruby-forum.com/t/problem-validating-boolean/103072 "2007-05-01T14:00:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ashley\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@Ashley\_M](https://www.ruby-forum.com/u/Ashley_M)\
**Post date:** [May 1, 2007, 2:00pm UTC](https://www.ruby-forum.com/t/problem-validating-boolean/103072/1 "2007-05-01T14:00:53Z")

</div>

Hi

I’ve got a problem validating a boolean and I can’t see what the  
problem is. Here’s my model:

class FinanceAgreement \< ActiveRecord::Base  
belongs\_to :asset

```
 validates_presence_of :finance_company
 validates_inclusion_of :balance, :in => 0.01..10_000_000
 validates_inclusion_of :term, :in => 1..600
 validates_numericality_of :term, :only_integer => true
 validates_inclusion_of :linked_to_residual_value_of_asset, :in

```

=\> [true, false]  
end

and here’s the RSpec code I’m using to test it:

context “A FinanceAgreement with valid attributes” do  
setup do  
@finance\_agreement = FinanceAgreement.new(:finance\_company =\>  
“Jigsaw Finance”, :start\_date =\> nil,  
:balance =\>  
5000, :term =\> 36, :linked\_to\_residual\_value\_of\_asset =\> true)  
end

```
 aspect "validation" do
   specify "should be valid" do
     @finance_agreement.should be_valid
   end

   specify "should be invalid if

```

linked\_to\_residual\_value\_of\_asset is not true or false" do  
@finance\_agreement.linked\_to\_residual\_value\_of\_asset = true  
@finance\_agreement.should be\_valid

```
     @finance_agreement.linked_to_residual_value_of_asset = false
     @finance_agreement.should be_valid

     @finance_agreement.linked_to_residual_value_of_asset = 1
     @finance_agreement.should_not be_valid
   end
 end

```

end

The first two expectations (true and false should be valid values)  
pass but it fails on this error:

should be invalid if linked\_to\_residual\_value\_of\_asset is not true  
or false  
expected valid? to return false, got true

/Users/ashleymoran/Documents/Development/YourMoney/trunk/src/spec/  
models/finance\_agreement\_spec.rb:65:

(I cut some specs out, line 65 is “@finance\_agreement.should\_not  
be\_valid”)

Any idea why 1 makes the model valid? The only thing I can find that  
makes it (correctly) invalid is nil.

Thanks  
Ashley

---

<div class="post-metadata">

**Author:** ![Ashley\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@Ashley\_M](https://www.ruby-forum.com/u/Ashley_M)\
**Post date:** [May 6, 2007, 3:03pm UTC](https://www.ruby-forum.com/t/problem-validating-boolean/103072/2 "2007-05-06T15:03:37Z")

</div>

Well, I would say it’s probably because in a lot of programming  
languages  
(and in a MySQL database), a 1 counts as true and a 0, false. Ruby is  
strongly typed, so it normally doesn’t automatically convert things so  
easily, but I expect rails is configured to convert numbers to boolean  
because it has to accept values from databases which may not have  
strictly  
boolean objects. There’s not a lot you can do about it. You could work  
something into before\_validation or after\_validation, it’s not worth it  
IMO.  
Just re-write your tests.

---

<div class="post-metadata">

**Author:** ![Ashley\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@Ashley\_M](https://www.ruby-forum.com/u/Ashley_M)\
**Post date:** [May 8, 2007, 9:17am UTC](https://www.ruby-forum.com/t/problem-validating-boolean/103072/3 "2007-05-08T09:17:53Z")

</div>

On 5/1/07, Ashley M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Any idea why 1 makes the model valid? The only thing I can find that  
> makes it (correctly) invalid is nil.

Hi Ashley,

ActiveRecord casts the values when you pull them out via the normal  
readers. So, if you have a class Flag with a boolean attribute #value:

> > flag.value = ‘1’  
> > =\> “1”  
> > flag.value  
> > =\> true  
> > flag.value = ‘Wibble.’  
> > =\> “Wibble.”  
> > flag.value  
> > =\> false

In rails 1.2.3, a `value’ is converted to a boolean by:

%w(true t 1).include?(value.to\_s.downcase)

If you want the value before it’s been cast, you can append  
“\_before\_type\_cast” to the accessor name:

> > flag.value = ‘Wibble.’  
> > =\> “Wibble.”  
> > flag.value\_before\_type\_cast  
> > =\> “Wibble.”

Not sure how useful that is to test, really, though.

Regards,  
George.
