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

  • Home
  • SEARCH
  • 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 8371493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:10:25+00:00 2026-06-09T14:10:25+00:00

So I was wondering if we can have a conditional allow_nil option for a

  • 0

So I was wondering if we can have a conditional allow_nil option for a validation on a rails model.

What I would like to do is to be able to allow_nil depending upon some logic(some other attribute)

So I have a product model which can be saved as draft. and when being saved as draft the price can be nil, but when not a draft save the price should be numerical. how can I achieve this. The following doesn’t seem to work. It works for draft case but allows nil even when status isn’t draft.

class Product<ActiveRecord::Base
   attr_accessible :status, price
   validates_numericality_of :price, allow_nil: :draft?

   def draft?
     self.status == "draft"
   end

end

Looking at rails docs I looks like there isn’t an option to pass a method to allow_nil?

One possible solution is to do separate validations for both cases

 with_options :unless => :draft? do |normal|
    normal.validates_numericality_of :price
 end

 with_options :if => :draft? do |draft|
   draft.validates_numericality_of :price, allow_nil: true
 end

Any other ways to get this working ?

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-06-09T14:10:27+00:00Added an answer on June 9, 2026 at 2:10 pm

    You can use if and unless to do the following

    class Product<ActiveRecord::Base
       attr_accessible :status, price
       validates_numericality_of :price, allow_nil: true, if: :draft?
       validates_numericality_of :price, allow_nil: false, unless: :draft?
    
       def draft?
         self.status == "draft"
       end
    
    end
    

    With the above, you will have 2 validations set up, one that applies when draft? == true, which will allow nils, and one where draft? == false which will not allow nils

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

Sidebar

Related Questions

I have a very repetitive conditional sentence . I was wondering if can be
I have a Catalyst application and would like to redirect based on a conditional
i was wondering if you can have conditional statments inside a heredocs, this is
I'm new to JQuery and was wondering how can I have Changes saved! displayed
I'm kind of new to JQuery and was wondering how can I have the
I was wondering how can convert CGcolorRef to UIColor ? I have created a
Just wondering how I can paste an object after I have selected it in
I am wondering how i can find all rows that have the same ID
I was wondering if anyone can point me in the right direction. I have
I have a UIImageview in my application and I was wondering how I can

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.