title: Controlling the order of rails validations
body: <p>I have a rails model which has 7 numeric attributes filled in by the user via a form.</p>
  
  <p>I need to validate the presence of each of these attributes which is obviously easy using</p>
  
  <pre><code>validates :attribute1, :presence =&gt; true
  validates :attribute2, :presence =&gt; true
  # and so on through the attributes
  </code></pre>
  
  <p>However I also need to run a custom validator which takes a number of the attributes and does some calculations with them. If the result of these calculations is not within a certain range then the model should be declared invalid.</p>
  
  <p>On it's own, this too is easy</p>
  
  <pre><code>validate :calculations_ok?
  
  def calculations_ok?
    errors[:base] &lt;&lt; "Not within required range" unless within_required_range?
  end
  
  def within_required_range?
    # check the calculations and return true or false here
  end
  </code></pre>
  
  <p>However the problem is that the method "validate" always gets run before the method "validates". This means that if the user leaves one of the required fields blank, rails throws an error when it tries to do a calculation with a blank attribute.</p>
  
  <p>So how can I check the presence of all the required attributes first?</p>