0
我有多個變量,我有問題排隊在一張桌子。每個年份我都有一個分組,但是如果只有一個列的結果生成時,它會將它放在第一行,而不是相應的年份。Rails多個變量
下面是它正在做的事情的圖像。旱地欄應該是在2006年。
查看
<% @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')