2015-09-07 67 views
0

我有多個變量,我有問題排隊在一張桌子。每個年份我都有一個分組,但是如果只有一個列的結果生成時,它會將它放在第一行,而不是相應的年份。Rails多個變量

下面是它正在做的事情的圖像。旱地欄應該是在2006年。

enter image description here

查看

<% @all.zip(@irrigated, @semi, @dryland) do |a, b, c, d| %> 
<tr> 
    <td><%= a.year %></td> 
    <td><% if a.nil? %>0<% else %><%= a.trial_id %><% end %></td> 
    <td><% if a.nil? %>0<% else %><%= "%.2f" % (a.lint/227) %><% end %></td> 
    <td><% if b.nil? %>0<% else %><%= b.trial_id %><% end %></td> 
    <td><% if b.nil? %>0<% else %><%= "%.2f" % (b.lint/227) %><% end %></td> 
    <td><% if c.nil? %>0<% else %><%= c.trial_id %><% end %></td> 
    <td><% if c.nil? %>0<% else %><%= "%.2f" % (c.lint/227) %><% end %></td> 
    <td><% if d.nil? %>0<% else %><%= d.trial_id %><% end %></td> 
    <td><% if d.nil? %>0<% else %><%= "%.2f" % (d.lint/227) %><% end %></td> 
    </tr> 
<% end %> 

控制器

@variety = Variety.where(variety_id: params[:variety_select]).group('variety_name') 

@all = Result.where(variety_id: params[:variety_select]).group('results.year').where('results.lint > 0').select('AVG(results.lint) AS lint, results.year as year, COUNT(results.trial_id) AS trial_id').joins(:trial) 

@irrigated = Result.where(variety_id: params[:variety_select]).group('results.year').where('results.lint > 0').select('AVG(results.lint) AS lint, results.year as year, COUNT(results.trial_id) AS trial_id').joins(:trial).where('trials.irrigated = ?', '1') 

@dryland = Result.where(variety_id: params[:variety_select]).group('results.year').where('results.lint > 0').select('AVG(results.lint) AS lint, results.year as year, COUNT(results.trial_id) AS trial_id').joins(:trial).where('trials.irrigated = ?', '0') 

@semi = Result.where(variety_id: params[:variety_select]).group('results.year').where('results.lint > 0').select('AVG(results.lint) AS lint, results.year as year, COUNT(results.trial_id) AS trial_id').joins(:trial).where('trials.irrigated = ?', '2') 

回答

0

我首先會檢查一下實際的查詢返回。你在2004年和2005年在Drylands有記錄嗎?它似乎並不如此。

也許你可以做到這一點

  • 一個永遠不會爲零,否則a.year會崩潰,所以沒有必要a.nil?
  • 爲其他值(B,C,d),您可以檢查if !b.nil? and b.year == a.year ...

這應該只在各自的年度行打印值