我有多个变量,我在表格中排列问题。我按年分组,但是当生成表时,如果其中一列只有一个结果,则将其放在第一行,而不是相应的年份。
这是它正在做的事情的图像。旱地专栏应该在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')
答案 0 :(得分:0)
我首先会检查查询实际返回的内容。你有2004年和2005年的旱地记录吗?它似乎并非如此。
也许你可以这样做
if !b.nil? and b.year == a.year ...
这应该只打印相应年份行中的值