Forum: Ruby on Rails Problem validating boolean

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
A90204c955db033cd975f7bb0ec9600b?d=identicon&s=25 Ashley Moran (Guest)
on 2007-05-01 14:00
(Received via mailing list)

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]

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

   context "A FinanceAgreement with valid attributes" do
     setup do
       @finance_agreement = =>
"Jigsaw Finance", :start_date => nil,
                                                 :balance =>
5000, :term => 36, :linked_to_residual_value_of_asset => true)

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

       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

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


(I cut some specs out, line 65 is "@finance_agreement.should_not

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

D5145c421cd25af6fa577c15219add90?d=identicon&s=25 unknown (Guest)
on 2007-05-06 15:03
(Received via mailing list)
Well, I would say it's probably because in a lot of programming
(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
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
Just re-write your tests.
C515daf003a781a638d8a01e41a935a0?d=identicon&s=25 George (Guest)
on 2007-05-08 09:17
(Received via mailing list)
On 5/1/07, Ashley Moran <> 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.

This topic is locked and can not be replied to.