2015-09-04 28 views
0

我有問題對齊表中的多個變量。如果不顯示'0',我想匹配每列的年份並顯示數據(如果存在)。 IVE附着的發生了什麼的圖像,列D的數據應是在2011年enter image description here多個變量不對齊

<% @a.zip(@b, @c, @d) do |a, b, c, d| %> 
    <tr> 
     <td><%= a.year %></td> 

     <td><% if a.nil? %>0<% else %><%= a.id %><% end %></td> 
     <td><% if a.nil? %>0<% else %><%= "%.2f" % (a.avg/227) %><% end %></td> 

     <td><% if b.nil? %>0<% else %><%= b.id %><% end %></td> 
     <td><% if b.nil? %>0<% else %><%= "%.2f" % (b.avg/227) %><% end %></td> 

     <td><% if c.nil? %>0<% else %><%= c.year %> <%= c.id %><% end %></td> 
     <td><% if c.nil? %>0<% else %><%= "%.2f" % (c.avg/227) %><% end %></td> 

     <td><% if d.nil? %>0<% else %><%= d.id %><% end %></td> 
     <td><% if d.nil? %>0<% else %><%= "%.2f" % (d.avg/227) %><% end %></td> 

    </tr> 
    <% end %> 

控制器

@a = Result.where(id: params[:id_select]).group('year').where('data > 0').select('AVG(data) AS data, year as year, COUNT(id) AS id').order('year ASC') 

    @b = Result.where(id: params[:id_select]).group('results.year').where('data > 0').select('AVG(data) AS data, year as year, COUNT(id) AS id').where('id = ?', '0').order('year ASC') 

    @c = Result.where(id: params[:id_select]).group('results.year').where('data > 0').select('AVG(data) AS data, year as year, COUNT(id) AS id').where('id = ?', '1').order('year ASC') 

    @d = Result.where(id: params[:id_select]).group('results.year').where('data > 0').select('AVG(data) AS data, year as year, COUNT(id) AS id').where('id = ?', '2').order('year ASC') 
+0

心中把你正在歌廳 –

+0

的形象是什麼,我通過代碼獲取的屏幕截圖。 D列的數據應該在2011年(平均2.04,Id爲1),但在2007年首次出現。 – DollarChills

+0

爲什麼你有兩個'where'子句可以設置對'id'的期望? – Jon

回答

1

@d陣列可能僅具有一個元素(2011年的數據) 。

當你與其他更大的陣列zip它,數據與其它2007年業績有關。

如:

2.1.5 :001 > a = [1,2,3] 
=> [1, 2, 3] 
2.1.5 :002 > b = [4,5,6] 
=> [4, 5, 6] 
2.1.5 :003 > c = [7] 
=> [7] 
2.1.5 :004 > a.zip(b,c) 
=> [[1, 4, 7], [2, 5, nil], [3, 6, nil]] 

你需要用一年查詢數據庫,而不是僅僅要求無論數據是可用的ID。