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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:24:26+00:00 2026-05-15T16:24:26+00:00

I have a problem where an :if clause within an ActiveRecord validation is not

  • 0

I have a problem where an :if clause within an ActiveRecord validation is not being honoured.

My model has an ip_port attribute which I am validating as being present, numeric and within a certain range. I am trying to ensure that each condition only generates one error. I do not want the situation where an empty attribute results in three messages displayed to the user stating that it is not present, required and not numeric.

This is my model as it stands

class Arc < ActiveRecord::Base
  attr_accessible :ip_port

  validates_presence_of :ip_port
  validates_numericality_of :ip_port, :allow_blank => true
  validates_inclusion_of :ip_port, :in => 1025..65535, :allow_blank => true, 
     :if => Proc.new {|arc| arc.ip_port.to_s.match(/^\d+$/) }
end

And this is my model spec and its results.

describe Arc do
  it "should be valid with valid attributes" do
    Arc.new(:ip_port => 1200).should be_valid
  end
  it "should be invalid with a non-numberic port" do
    Arc.new(:ip_port => "test").should be_invalid
  end
  it "should be invalid with a missing port" do
    Arc.new(:ip_port => nil).should be_invalid
  end
  it "should have one error with a missing port" do
    a = Arc.new(:ip_port => nil)
    a.should be_invalid
    a.should have(1).errors_on(:ip_port)
  end
  it "should have one error with a non-numeric port" do
    a = Arc.new(:ip_port => "test")
    a.should be_invalid
    a.should have(1).errors_on(:ip_port)
  end
  it "should have one error with a numeric port outside the range" do
    a = Arc.new(:ip_port => 999)
    a.should be_invalid
    a.should have(1).errors_on(:ip_port)
  end
end
Arc
- should be valid with valid attributes
- should be invalid with a non-numberic port
- should be invalid with a missing port
- should have one error with a missing port
- should have one error with a non-numeric port (FAILED - 1)
- should have one error with a numeric port outside the range

1)
'Arc should have one error with a non-numeric port' FAILED
expected 1 errors on :ip_port, got 2
./spec/models/arc_spec.rb:21:

Finished in 0.108245 seconds

My question is why I am getting two errors for a non-numeric ip_port when the :if clause should prevent the validates_inclusion of from being called.

This is Rails 2.3.5 with Ruby 1.8.7 on OS/X 10.6.3

  • 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-15T16:24:27+00:00Added an answer on May 15, 2026 at 4:24 pm

    Whilst having a contemplative stroll I have solved my own problem.

    The issue is that in order to validate the inclusion within the range it converts the provided value to an int and then checks for inclusion. So for a non-numeric value I will get both a :not_a_number and an :inclusion error.

    The answer is to modify the :if clause to use the value before it was typecast so my validates_inclusion_of method becomes

    validates_inclusion_of :ip_port, :in => 1025..65535, :allow_blank => true, 
      :if => Proc.new {|arc| arc.ip_port_before_type_cast.to_s.match(/^\d+$/) }
    

    This then gives me one error for each of three conditions.

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

Sidebar

Ask A Question

Stats

  • Questions 478k
  • Answers 478k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You should insert your div in the document first. Or… May 16, 2026 at 5:26 am
  • Editorial Team
    Editorial Team added an answer printf("d\n",c); /// Means just print "d" (c is ignored) printf("%d\n",c);… May 16, 2026 at 5:26 am
  • Editorial Team
    Editorial Team added an answer Got the solution using this reference i extended the AuthComponent… May 16, 2026 at 5:26 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.