Controlling the order of rails validations

I’m not sure it’s guaranteed what order these validations get run in, as it might depend on how the attributes hash itself ends up ordered. You may be better off making your validate method more resilient and simply not run if some of the required data is missing. For example:

def within_required_range?
  return if ([ a, b, c, d ].any?(&:blank?))

  # ...
end

This will bail out if any of the variables a through d are blank, which includes nil, empty arrays or strings, and so forth.

Leave a Comment