我使用rails做了一个应用程序的范围,我正在尝试创建一个动态参数,不要重复我每年的所有时间,我只是想从我的控制器中选择年份和查询。
这是我的表:
|policies|
|id| |date_ini| |date_end|
1 2013-01-01 2014-01-01
2 2012-02-01 2013-02-01
3 2013-03-01 2013-03-03
4 2013-04-01 2013-08-01
以下是范围:
date_ini <= range_of_each_policy <= date_end
以下是使用2013年的逻辑:
|jan| |feb| |mar| |apr| |may| |jun| |jul| |ago| |sep| |oct| |nov| |dic|
id1 id1 id1 id1 id1 id1 id1 id1 id1 id1 id1 id1
id2 id2 __________________________________________________________
__________ id3 ______________________________________________________
_______________ id4 id4 id4 id4 id4 _______________________
每月也会显示:
jan feb mar apr.........sep oct nov dec
2 2 2 2.... 1 1 1 1
这是查询它使用mysql工作的总和:
SET @year := 2013;
SELECT
SUM(CONCAT(@year, '-01-01') BETWEEN date_ini AND date_end) Jan,
SUM(CONCAT(@year, '-02-01') BETWEEN date_ini AND date_end) Feb,
SUM(CONCAT(@year, '-03-01') BETWEEN date_ini AND date_end) Mar,
SUM(CONCAT(@year, '-04-01') BETWEEN date_ini AND date_end) Apr,
SUM(CONCAT(@year, '-05-01') BETWEEN date_ini AND date_end) May,
SUM(CONCAT(@year, '-06-01') BETWEEN date_ini AND date_end) Jun,
SUM(CONCAT(@year, '-07-01') BETWEEN date_ini AND date_end) Jul,
SUM(CONCAT(@year, '-08-01') BETWEEN date_ini AND date_end) Aug,
SUM(CONCAT(@year, '-09-01') BETWEEN date_ini AND date_end) Sep,
SUM(CONCAT(@year, '-10-01') BETWEEN date_ini AND date_end) Oct,
SUM(CONCAT(@year, '-11-01') BETWEEN date_ini AND date_end) Nov,
SUM(CONCAT(@year, '-12-01') BETWEEN date_ini AND date_end) `Dec`
FROM
policies
WHERE
date_end >= CONCAT(@year, '-01-01')
AND
date_ini <= CONCAT(@year, '-12-01')
以下是链接http://sqlfiddle.com/#!2/dfcd5/2
这是我的控制器:
@year= params[:year]
@var =Policy.find_by_sql("SELECT
SUM(CONCAT(@year, '-01-01') BETWEEN date_ini AND date_end) as jan,
SUM(CONCAT(@year, '-02-01') BETWEEN date_ini AND date_end) as feb,
SUM(CONCAT(@year, '-03-01') BETWEEN date_ini AND date_end) as mar,
SUM(CONCAT(@year, '-04-01') BETWEEN date_ini AND date_end) as apr,
SUM(CONCAT(@year, '-05-01') BETWEEN date_ini AND date_end) as may,
SUM(CONCAT(@year, '-06-01') BETWEEN date_ini AND date_end) as jun,
SUM(CONCAT(@year, '-07-01') BETWEEN date_ini AND date_end) as jul,
SUM(CONCAT(@year, '-08-01') BETWEEN date_ini AND date_end) as aug,
SUM(CONCAT(@year, '-09-01') BETWEEN date_ini AND date_end) as sep,
SUM(CONCAT(@year, '-10-01') BETWEEN date_ini AND date_end) as oct,
SUM(CONCAT(@year, '-11-01') BETWEEN date_ini AND date_end) as nov,
SUM(CONCAT(@year, '-12-01') BETWEEN date_ini AND date_end) as `dec`
FROM
policies
WHERE
date_end >= CONCAT(@year, '-01-01')
AND
date_ini <= CONCAT(@year, '-12-01') )"
以下是我的观点:
<% form_tag :controller=>"policy_management/policy",:action=>"generate_statistical_report" do %>
Select year:
<%= select_tag "year",options_for_select([ ["2012",2012],["2013",2013],["2014",2014]],params[:year].to_i) %>
<%= submit_tag "Buscar", :name => nil %>
<% end %>
<%= @var[0].try(:jan) %>
这是我的日志:
Policy Load (3.2ms) SELECT
SUM(CONCAT(@year, '-01-01') BETWEEN date_ini AND date_expired) as jan,
SUM(CONCAT(@year, '-12-01') BETWEEN date_ini AND date_expired) `Dec`
FROM
policies
WHERE
date_expired >= CONCAT(@year, '-01-01')
AND
date_ini <= CONCAT(@year, '-12-01')
我视线中的选择框没有通过我的参数@year
请有人帮我这个吗?
我真的很感激帮助或想法。
答案 0 :(得分:1)
您需要使用字符串插值"#{@year}"