Prev | Current Page 51 | Next

David Berube

"Practical Reporting with Ruby and Rails"

sql. (Note that this listing increments the database
version number, so you can still go back and run the previous examples, which have different
database structures.) Next, run the following command:
mysql -u your_mysql_username < player_schema_3.sql
Now that you have the data loaded into your database, let??™s tackle the first question.
CHAPTER 2 n CALCULATING STATISTICS WITH ACTIVE RECORD 24
Calculating Salary Distribution
You want to find how many players are at each salary rate. It??™s reasonably simple to use
Active Record??™s count function and grouping ability to calculate the distribution of the
salary, and Listing 2-2 does just that.
Listing 2-2. Calculating Salary Distribution (salary_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:
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63