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 7992871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:41:38+00:00 2026-06-04T13:41:38+00:00

In a Rails app I have several integer attributes on a model. A user

  • 0

In a Rails app I have several integer attributes on a model.

A user should be able to create a record and leave these attributes blank.

Or, if the user enters values for these attributes, they should be validated for numericality and within a certain range.

In the model I have something like this

validates_presence_of :name    
validates_numericality_of :a, :only_integer => true, :message => "can only be whole number."
validates_inclusion_of :a, :in => 1..999, :message => "can only be between 1 and 999."

If I now test with the minimum required attributes to save:

factory :model do
  sequence(:name) { |n| "model#{n}" }
end

it "should save with minium attributes" do
  @model = FactoryGirl.build(:model)
  @model.save.should == false
end

I get

Validation failed: a can only be whole number., a can only be between 1 and 999.

How can I validate numericality and inclusion only if a value is given for :a, while still allowing :a to be nil in some cases?

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-04T13:41:40+00:00Added an answer on June 4, 2026 at 1:41 pm

    You can add an :allow_nil => true to your validates_numericality_of.

    validates_numericality_of :a, :only_integer => true, :allow_nil => true, 
        :message => "can only be whole number."
    

    You can also use greater_than_or_equal_to and less_than_or_equal_to options if you just want to use one validation:

    validates_numericality_of :a, :only_integer => true, :allow_nil => true, 
        :greater_than_or_equal_to => 1,
        :less_than_or_equal_to => 999,
        :message => "can only be whole number between 1 and 999."
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several active record models in a rails app and I would like
The main idea is that I have several worker instances of a Rails app,
I have a Rails app representing a card game. There are several different types
I have a rails app with several views and layouts. One of my layouts
I've a mongoid embedded one to one model in a Rails app (User -->
I currently have a Rails 3.1 app that hosts multiple sites for several different
We have already built a rails app that has several users and an image
I have some problem trying to understand when building a rails app with several
I have a rails app with several models. I have a function that I
My rails app has a user model that has a HABTM relationship with interests

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.