Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7056479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:52:20+00:00 2026-05-28T03:52:20+00:00

Having a rfq model in the app. There are two fields. One is need_report

  • 0

Having a rfq model in the app. There are two fields. One is need_report which is a boolean. Another one is report_language which is a string. The logic is if need_report is true, then there should be an entry in report_language. Otherwise, if need_report if false, report_language could be empty. Here is the code in rfq.rb:

  validates :need_report, :presence => true
  validates_inclusion_of :need_report, :in => [true, false]  
  validates :report_language, :presence => {:if => :need_report?}

  def need_report?
    need_report
  end

However the following rspec case failed:

  it "should be OK for nil report_language if need_report is false" do
    rfq = Factory.build(:rfq, :need_report => false, :report_language => nil)
    rfq.should be_valid    
  end

The error is that the rfq is not valid:

  1) Rfq should be OK for nil report_language if need_report is false
     Failure/Error: rfq.should be_valid
       expected valid? to return true, got false
     # ./spec/models/rfq_spec.rb:57:in `block (2 levels) in <top (required)>'

This case could pass if “validates :need_report, :presence => true” is removed from the model. It seems that if need_report is true, then report_language can not be empty.

Any thoughts about the problem? Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:52:20+00:00Added an answer on May 28, 2026 at 3:52 am

    You can’t use validates_presence_of or validates :column, :presence => true to check if boolean columns are empty.

    http://api.rubyonrails.org/classes/ActiveModel/Validations/HelperMethods.html#method-i-validates_presence_of

    Answer: Instead you need to use validates_inclusion_of and specify an array of accepted inputs, which you already have. This should be sufficient validation for what you want to do.

    Explanation: Your first validation is seeing false in the column (which, in Ruby is equivalent to nil). It then runs .blank? on nil and gets back true (false == nil & nil.blank? == true), meaning it thinks the column is blank and it throws an error.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having successfully got a WCF service and client to talk to one another using
Having been a PHP developer on LAMP servers for quite a while, is there
Having a heckuva time with this one, though I feel I'm missing something obvious.
Having difficulty articulating this correlated subquery. I have two tables fictitious tables, foo and
Having read this past question for git, I would like to ask if there
Having a headache with IE. I have an image (24x24) which I'd like to
Having a problem with Scala import. I have an import problem with two classes.
Having two lists [A,B,C,D] and [1,2,3,4] , I want to provide a user interface
Having a look on the PHP documentation, the following two methods of the DateTime
Having a table with the following fields: Order,Group,Sequence it is required that all orders

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.