quantity > highest_win.quantity
end
puts "#{game.name}: #{highest_win.player.name} with #{highest_win.quantity} wins"
end
As you can see, it uses the aforementioned wins property of each Game object. The
method returns an array of wins for the current game, so we loop through each and find
the win with the highest quantity. At that point, the name of the game is printed out, as
well as the name of the winning player and the quantity.
Next, a very similar loop goes through all of the players and finds the player with the
highest total wins:
players = Player.find(:all)
highest_winning_player = nil
players.each do |player|
highest_winning_player = player if
highest_winning_player.nil? or
player.total_wins > highest_winning_player.total_wins
end
CHAPTER 1 n DATA ACCESS FUNDAMENTALS 16
puts "Highest Winning Player: #{highest_winning_player.name} " <<
"with #{highest_winning_player.total_wins} wins"
You loop through each player and use the total_wins method to sum the player??™s
quantity of wins. The player with the highest result from total_wins is selected, and that
player??™s name and total wins are printed out.
Pages:
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52