Prev | Current Page 32 | Next

David Berube

"Practical Reporting with Ruby and Rails"


The methods you use to set your fields??”name and salary??”are provided by Active
Record, and they are named after their associated fields. Remember that both getter and
setter methods are automatically created for each field name declared in your schema
(Listing 1-1).
After you create the first player, you create two more in similar fashion. Then you
need to perform the analysis:
salary_total = 0
win_total = 0
players = Player.find(:all)
players.each do |player|
puts "#{player.name}: $#{'%0.2f' % (player.salary/player.wins)} per win"
salary_total = salary_total + player.salary
win_total = win_total + player.wins
end
puts "\nAverage Cost Per Win : $#{'%0.2f' % (salary_total / win_total )}"
CHAPTER 1 n DATA ACCESS FUNDAMENTALS 10
This code finds all of the players using the Player.find class method (inherited from
ActiveRecord::Base) and saves them into an array. It then loops through the array while
totaling the salaries and wins. For each player, it prints out the player??™s salary/wins ratio??”
that is, how much the player costs the company for each win.


Pages:
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
wyciągarki wróżby miłosne cukiernia-stykowscy.pl salon kosmetyczny kraków pompy ciepła