1 => допустим, есть хеш x
x = {["Category1", 2018]=>54,
["Category1", 2019]=>187,
["Category2", 2017]=>43,
["Category2", 2018]=>84,
["Category2", 2019]=>239
}
2 => создать массив [[category, year, count]...]
category_year_counts = []
x.each do |key, value|
category_year_counts << [key[0], key[1], value]
end
#=>[["Category1", 2018, 54], ["Category1", 2019, 187], ["Category2", 2017, 43], ["Category2", 2018, 84], ["Category2", 2019, 239]]
3 => Вот оно -
<% x.each do |key, value| %>
<tr>
<td>categories</td>
<%years = x.keys.map{|elm| elm[1]}.uniq%>
<%years.each do |year|%>
<td><%=year%></td>
<%end%>
<tr>
<%categories_lists = x.keys.map{|elm| elm[0]}.uniq%>
<%categories_lists.each do |category|%>
<tr>
<td><%=category%></td>
<%years.each do |year|%>
<%result= category_year_counts.select{|elm| elm[0].eql?(category) && elm[1].eql?(year)}.first%>
<td><%=result.present? && result[2].present? ? result[2] : ""%></td>
<%end%>
</tr>
<%end%>
<%end%>
Может быть более удобный способ сделать это.