0

それぞれがラウンドロビントーナメント構造のラウンドに属するMatchオブジェクトの配列があるとしましょう...

一致する

Round     | Registrant_ID     |Registrant_ID_2    |Winner_id
1         |    1              |    2              |   2 
1         |    3              |    4              |   4
1         |    5              |    6              |   5
1         |    7              |    8              |   8
2         |    1              |    4              |   1
2         |    3              |    6              |   3
2         |    5              |    8              |   5
2         |    7              |    2              |   2
3         |    1              |    6              |   1
...

私がやりたいのは、すべての一致をラウンドごとにグループ化し、そのラウンドをループして一致をリストすることです。

必要な出力は次のようになります...

 <h1>Round 1</h1>    
 <table>
 <thead>
  <tr>
   <th>Player 1</th>
   <th>Player 2</th>
   <th>Winner</th>
  </tr>
 </thead>
 <tbody>
  <tr>
    <td>1</td>
    <td>2</td>
    <td>2</td>
  </tr>
  <tr>
    <td>3</td>
    <td>4</td>
    <td>4</td>
  </tr>
  <tr>
    <td>5</td>
    <td>6</td>
    <td>5</td>
  </tr>
  <tr>
    <td>7</td>
    <td>8</td>
    <td>8</td>
  </tr>
 </tbody>
</table>

私が直面している問題は、ラウンド属性によって一致レコードをループする方法がわからないことです。ラウンドに参加するプレイヤーの数はさまざまであるため、 in_groups_ofのようなものを使用できるかどうかはわかりませんが、ここに示すように、常に8になるとは限りません。

これまでのところ、すべてのレコードをループして、一致するたびにテーブルを作成するコードを次に示します(個々のラウンドのテーブルを探しています)。

- @matches.each do |match|
  %h1= "Round #{match.round}"
  %table.table.table-bordered
    %thead
      %tr
        %th.span4 Player 1
        %th.span4 Player 2
        %th.span4 Winner
    %tbody
      %tr
        %td= match.register.user.username
        %td= match.register_2.user.username
        %td= match.winner.user.username unless match.winner.nil?

出力の意味は次のとおりです。個別のラウンドごとに個別のテーブルがあることに注意してください。

ラウンドロビンマッチリスト

4

1 に答える 1

1

たぶん、あなたはこのようにラウンドごとに試合をグループ化することを意味します:

- @matches.group_by(&:round).each do |round, matches|
  %h1= "Round #{round}"
  %table.table.table-bordered
    %thead
    %tr
      %th.span4 Player 1
      %th.span4 Player 2
      %th.span4 Winner
    %tbody
      - matches.each do |match|
        %tr
          %td= match.register.user.username
          %td= match.register_2.user.username
          %td= match.winner.user.username unless match.winner.nil?
于 2012-09-14T20:35:58.593 に答える