Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

437
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda