如何对group_by月份和年份进行排序?

时间:2016-08-31 04:25:12

标签: ruby-on-rails ruby sorting group-by

pages_controller.rb

@past_challenges_by_years = @past_challenges.group_by { |t| [t.deadline.year, t.deadline.month] }

如何在视图中将其分解为年份,然后将其细分为月份,如下所示:

2014 # Years
 01 # Months
   Challenge
   Challenge
 02
   Challenge
 08
   Challenge
2016
 03
   Challenge
 08
   Challenge

view.html.erb

<% @past_challenges_by_years.sort.each do |year, challenges| %>
  <%= year %>
  <%= month %> # I don't know how to define this.
  <% for challenge in challenges %>
    etc...
  <% end %>
<% end %>

1 个答案:

答案 0 :(得分:1)

我首先对密钥进行排序,然后在迭代这些密钥时查找挑战。

https://userinfo.yahooapis.jp/yconnect/v1/attribute?schema=openid
Authorization: Bearer accessToken

编辑:这是一个只显示年份和月份的新解决方案。

<% @past_challenges_by_years.keys.sort.each do |(year, month)| %>
  <%= year %>
  <br />
  <%= month %>
  <br />
  <% @past_challenges_by_years[[year, month]].each do |challenge| %>
    <%= challenge %>
    <br />

我认为转换为ERB应该很简单。