Prev | Current Page 27 | Next

David Berube

"Practical Reporting with Ruby and Rails"

(Of course, this may or may
not be true, but the goal of a report is to provide the data that the end user requests.)
Fortunately, Active Record makes this fairly easy. With Active Record and MySQL
installed, you can create a simple schema, populate it with your data, and then find the
average salary.
Listing 1-1 shows the code to create a player table schema.
Listing 1-1. Simple Player Table Schema (player_schema.sql)
CREATE DATABASE players;
USE players;
CREATE TABLE players (
id int(11) NOT NULL AUTO_INCREMENT,
CHAPTER 1 n DATA ACCESS FUNDAMENTALS 6
name TEXT,
wins int(11) NOT NULL,
salary DECIMAL(9,2),
PRIMARY KEY (id)
)
Save this file as player_schema.sql. Then run the following MySQL command:
mysql -u your_mysql_username -p < player_schema.sql
Next, you can write the code to declare a model to wrap the newly created database
table, establish a connection to the database, add a few records, and then calculate the
average win/salary ratio. Listing 1-2 shows this code.
Listing 1-2. Calculating Player Salaries (player_salary_ratio.


Pages:
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
hale A*Teens ubrania prezenty akcesoria motocyklowe