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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
njmacinnes-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-May-06 13:02 UTC
Re: Problem validating boolean
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. On 01/05/07, Ashley Moran <work-TvaLdgFUtx01pAEv6XV82Vpr/1R2p/CL@public.gmane.org> wrote:> > > 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 > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 5/1/07, Ashley Moran <work-TvaLdgFUtx01pAEv6XV82Vpr/1R2p/CL@public.gmane.org> 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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---