title: Rails API makes multiple database queries when similar code in Rails console only makes one database query
body: <p>I wrote a simple Rails API that returns all objects between two dates.  In Rails console, the database in queried only once, but the API is causing multiple queries for the same request.</p>
  
  <p>This is the controller method:</p>
  
  <pre><code>def historic_returns
    respond_to do |format|
      format.json do
        start_date = Date.new(params[:start_year].to_i, params[:start_month].to_i)
        end_date = Date.new(params[:end_year].to_i, params[:end_month].to_i)
        @result = ShillerDataMonth.records_between_two_dates(start_date, end_date)
        render :json =&gt; @result
      end
    end
  end
  </code></pre>
  
  <p>This is the class method in the model that the controller uses:</p>
  
  <pre><code>def self.records_between_two_dates(start_date, end_date)
    ShillerDataMonth.where("record_date &gt;= ? AND record_date &lt;= ?", start_date, end_date).order("record_date asc")
  end
  </code></pre>
  
  <p>When I run the ShillerDataMonth#records_between_two_dates method in Rails console, there is only one database query, as expected:</p>
  
  <pre><code>&gt;&gt; ShillerDataMonth.records_between_two_dates(Date.new(2010, 01), Date.new(2012, 01))
    ShillerDataMonth Load (2.2ms)  SELECT "shiller_data_months".* FROM "shiller_data_months" WHERE (record_date &gt;= '2010-01-01' AND record_date &lt;= '2012-01-01') ORDER BY record_date asc
  </code></pre>
  
  <p>However, when I visit this path on localhost /historic_returns.json?start_year=2010&amp;start_month=1&amp;end_year=2012&amp;end_month=1, there are multiple queries:</p>
  
  <pre><code>Started GET "/historic_returns.json?start_year=2010&amp;start_month=1&amp;end_year=2012&amp;end_month=1" for 127.0.0.1 at 2013-07-08 20:44:34 -0400
  Processing by ShillerDataMonthsController#historic_returns as JSON
    Parameters: {"start_year"=&gt;"2010", "start_month"=&gt;"1", "end_year"=&gt;"2012", "end_month"=&gt;"1"}
    ShillerDataMonth Load (2.1ms)  SELECT "shiller_data_months".* FROM "shiller_data_months" WHERE (record_date &gt;= '2010-01-01' AND record_date &lt;= '2012-01-01') ORDER BY record_date asc
    ShillerDataMonth Load (0.6ms)  SELECT "shiller_data_months".* FROM "shiller_data_months" WHERE "shiller_data_months"."year_month" = '2009.12' LIMIT 1
    CACHE (0.0ms)  SELECT "shiller_data_months".* FROM "shiller_data_months" WHERE "shiller_data_months"."year_month" = '2009.12' LIMIT 1
    CACHE (0.0ms)  SELECT "shiller_data_months".* FROM "shiller_data_months" WHERE "shiller_data_months"."year_month" = '2009.12' LIMIT 1
    CACHE (0.0ms)  SELECT "shiller_data_months".* FROM "shiller_data_months" WHERE "shiller_data_months"."year_month" = '2009.12' LIMIT 1
    ShillerDataMonth Load (0.6ms)  SELECT "shiller_data_months".* FROM "shiller_data_months" WHERE "shiller_data_months"."year_month" = '2010.01' LIMIT 1
    CACHE (0.0ms)  SELECT "shiller_data_months".* FROM "shiller_data_months" WHERE "shiller_data_months"."year_month" = '2010.01' LIMIT 1
    CACHE (0.0ms)  SELECT "shiller_data_months".* FROM "shiller_data_months" WHERE "shiller_data_months"."year_month" = '2010.01' LIMIT 1
    CACHE (0.0ms)  SELECT "shiller_data_months".* FROM "shiller_data_months" WHERE "shiller_data_months"."year_month" = '2010.01' LIMIT 1
    ShillerDataMonth Load (0.8ms)  SELECT "shiller_data_months".* FROM "shiller_data_months" WHERE "shiller_data_months"."year_month" = '2010.02' LIMIT 1
    CACHE (0.0ms)  SELECT "shiller_data_months".* FROM "shiller_data_months" WHERE "shiller_data_months"."year_month" = '2010.02' LIMIT 1
    CACHE (0.0ms)  SELECT "shiller_data_months".* FROM "shiller_data_months" WHERE "shiller_data_months"."year_month" = '2010.02' LIMIT 1
    CACHE (0.0ms)  SELECT "shiller_data_months".* FROM "shiller_data_months" WHERE "shiller_data_months"."year_month" = '2010.02' LIMIT 1
  </code></pre>
  
  <p>...this keeps going for every month....</p>
  
  <p>Why does the API have multiple requests?  How can I fix it?  Thanks for the help!</p>