title: How to perform math calculations in Active Record Querying in Rails
body: <p>I'm building an application and I'm finding it necessary to perform some simple math calculations in my query.  Essentially, I've got a database with daily values from the S&amp;P 500, and I need to get a listing of days depending on the criteria entered.</p>
  
  <p>The user inputs both a day range, and a % range.  For instance, if the date range is Jan 1/2013 - Apr 1/2013 and the % range is -1% - 1%, it should return a list of all S%P 500 daily values between the dates where difference between the opening and closing values are in the % range.</p>
  
  <p>The problem is that I don't actually have a column for %; I only have a column for opening/closing values.  It is simple enough to calculate the % given only the opening and closing values (close-open)/open*100.  But I'm not sure how to do this within the query.</p>
  
  <p>Right now the query is successfully searching within the date range.  My query is:</p>
  
  <pre><code>@cases = Close.find(:all, conditions:["date between ? and ?",
      @f_start, @f_end])
  </code></pre>
  
  <p>But how can I  get it to check if the current row's (close-open)/open*100 value is between the two % range values?</p>
  
  <p>Alternatively, if this is not possible or in bad practice, where should I be handling this?</p>