1

I'm trying to store FIFA Games, and set a scoreboard with a ranking system.

I shouldn't use logic in the view, but if I calculate them in the controller, it renders an error that the method user is not specified. When I put it in the loop, however, it recognizes it because the user is the looped item.

The app can already save games and calculate the winner. The app adds winner_id and loser_id to each game. Later in the scoreboard, I count how many current user_id's from the loop match all games' winner_id's and loser_id's. This keeps the database clean. I don't want to keep the wins and losses in the db because when a game is deleted, it shouldn't count as a win or loss anymore.

Controller:

class ScoreboardController < ApplicationController
    def index
        @users = User.all
    end
end

VIEW:

<div class="panel panel-default" style="margin-left: 10px; margin-right:10px">
  <!-- Default panel contents -->
  <div class="panel-heading">Scoreboard</div>

  <!-- Table -->
          <table class="table">
             <thead>
                 <th>#</th>
                 <th>Username</th>
                 <th>Ratio</th>
                 <th>Wins</th>
                 <th>Losses</th>
              </thead>

              <% @users.each do |user|%> 
                 <tbody> 

              <td>
               1 

              </td>

              <td>
                  <%= user.username %>
              </td>

                  <% if (Game.where(:winner_id => user.id).count) == 0 %>

                  <td>Unvalid</td>

                <% elsif (Game.where(:loser_id => user.id).count) == 0 %>

                     <td>Unvalid</td>

                     <% else %>   
                       <% @ratio =  (number_with_precision((((Game.where(:winner_id => user.id).count).to_f) / (Game.where(:loser_id => user.id).count).to_f), precision: 2))  %>

                     <td><%= @ratio %></td>


                     <% end %>

                <td>
                 <%= Game.where(:winner_id => user.id).count %>
              </td>
               <td>
                   <%= Game.where(:loser_id => user.id).count %>
              </td>


                     <% end %>
              </tbody>
          </table>

        </div>

I'd like to put this list in the right order. The list should be ordered by ratio. => the @ratio from the view. Can I do this directly?

In the first td, the current position is shown. It shows 1 for every user. How can I make this 1, 2, 3, ...?

2 Answers 2

4

You should add those methods in your User model.

class User < ActiveRecord::Base
  has_many :wins, class_name: 'Game', foreign_key: 'winner_id'
  has_many :losses, class_name: 'Game', foreign_key: 'loser_id'

  def ratio
    wins.count / losses.count.to_f * 100
  end
end

then in the controller :

def index
    @users = User.all.sort_by(&:ratio)
end

and in the view, use the user instance methods directly : <%= user.wins.count %>

Sign up to request clarification or add additional context in comments.

Comments

1

You should be doing it the way @ThomasHaratyk has suggested above.

Additional question: in the first td the current position is shown, for now it shows a 1 for every user, how can I make this 1, 2 , 3 , ... ?

<% @users.each_with_index do |user, index|%> 
   <tbody> 
      <td>
          <%= index + 1 %>
       </td>
<% end %>

Comments

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.