0

Lets say I have an array of Match objects, each belonging to a round in a round robin tournament structure...

Matches

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
...

What I wish to do is group all of the matches by Round and loop through that round and list the matches.

The desired output would be something like...

 <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>

The problem I'm facing is that I don't know how to loop through the match records by their round attribute. I'm not sure if something like in_groups_of can be used because the number of players that participate in a round will vary, it won't always be 8 as seen here.

Here is my code thus far which simply loops through all records and creates a table for every match(I'm looking for tables for individual rounds):

- @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?

Here's what the output is meant to look like, notice separate tables for separate rounds:

round robin match listing

1 Answer 1

1

Maybe you mean to group matches by round, like this:

- @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?
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Megas but that wasn't quite what I had in mind have a look at my screen-shot for a visual
You want one table but several sections in that table, am I right?
Nope, I need a table for each round.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.