Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

438
Views
Ruby 6 model validation for numericality: false

I'd like to make sure certain fields are populated only when the record has another flag set (and vice-versa). However my test is allowing the model to save even at points when I think it should fail.

class Measurement < ApplicationRecord

    validates :start_date, presence: true
    validates :end_date, presence: true
    validates :actual, numericality: true,    if: :is_baseline?      # baseline must have actual value
    validates :forecast, numericality: false, if: :is_baseline?      # baseline must not have forecast value
    validates :target, numericality: false,   unless: :is_target?    # only target can have target value
    validates :target, numericality: true,    if: :is_target?        # only target can have target value

    belongs_to :measure
    belongs_to :organisation

end

First test that fails:

  test "can't create an invalid baseline measure" do

    measurement = @measure.measurements.new
    measurement.organisation = @organisation
    measurement.start_date   =  "2022-01-1"
    measurement.end_date     = "2022-01-31"
    measurement.is_baseline  = true
    measurement.actual       = 1000
    measurement.forecast     = 1000 # baseline should not have a forecast

    assert_not measurement.save, "Invalid Baseline Measurement saved"
  end

Second test that fails

  test "can't create an invalid interval measure" do

    measurement = @measure.measurements.new
    measurement.organisation = @organisation
    measurement.start_date   =  "2022-01-1"
    measurement.end_date     = "2022-01-31"
    measurement.forecast     = 1000
    measurement.target       = 1000 # interval should not have a target

    assert_not measurement.save, "Invalid Interval Measurement saved"
  end

Any idea why these tests are failing?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

This is not how these validations work. numericality: false doesn't validate the opposite but turns the validation off.

But instead, you can use the absence validator that checks that a value is not set:

validates :actual,   numericality: true, if: :is_baseline?      # baseline must have actual value
validates :forecast, absence: true,      if: :is_baseline?      # baseline must not have forecast value
validates :target,   absence: true,      unless: :is_target?    # only target can have target value
validates :target,   numericality: true, if: :is_target?        # only target can have target value
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!