Prev | Current Page 53 | Next

David Berube

"Practical Reporting with Ruby and Rails"

The work actually performed by this script is done by the following
code:
Player.calculate(:count, :id, :group=>'salary').each do |player|
salary, count = *player
puts "$#{'%0.2f' % salary}\t#{count} "
end
Essentially, this code groups by the salary field and counts the ids in each group.
Since each player has a unique id, this is equivalent to counting the rows themselves. For
each row, the array is split into its component parts: salary and count. As before, the order
of the array passed to your block is the grouped fields followed by the calculated field.
Calculating Drink/Win Distribution
Next, let??™s try answering the second question: which drink leads to the highest performance?
Listing 2-3 uses find_by_sql to do that.
CHAPTER 2 n CALCULATING STATISTICS WITH ACTIVE RECORD 26
Listing 2-3. Calculating Drink/Win Distribution (drink_win_distribution.rb)
require 'active_record'
# Establish a connection to the database. . .
ActiveRecord::Base.establish_connection(
:adapter => 'mysql',
:host => 'localhost',
:username => 'root', # This is the default username and password
:password => '', # for MySQL, but note that if you have a
# different username and password,
# you should change it.


Pages:
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
Zgaga www.books74.tolkienlotr.com życzenia na walentynki obrączki ślubne utylizacja elektroniki