我编写了一个简单的 Rails API,它返回两个日期之间的所有对象。在 Rails 控制台中,数据库只查询一次,但 API 导致对同一请求进行多次查询。
这是控制器方法:
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 => @result
end
end
end
这是控制器使用的模型中的类方法:
def self.records_between_two_dates(start_date, end_date)
ShillerDataMonth.where("record_date >= ? AND record_date <= ?", start_date, end_date).order("record_date asc")
end
当我在 Rails 控制台中运行 ShillerDataMonth#records_between_two_dates 方法时,正如预期的那样,只有一个数据库查询:
>> 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 >= '2010-01-01' AND record_date <= '2012-01-01') ORDER BY record_date asc
但是,当我在 localhost /historic_returns.json?start_year=2010&start_month=1&end_year=2012&end_month=1 上访问此路径时,会出现多个查询:
Started GET "/historic_returns.json?start_year=2010&start_month=1&end_year=2012&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"=>"2010", "start_month"=>"1", "end_year"=>"2012", "end_month"=>"1"}
ShillerDataMonth Load (2.1ms) SELECT "shiller_data_months".* FROM "shiller_data_months" WHERE (record_date >= '2010-01-01' AND record_date <= '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
......每个月都这样......
为什么 API 有多个请求?我该如何解决?谢谢您的帮助!