Prev | Current Page 47 | Next

David Berube

"Practical Reporting with Ruby and Rails"

find_by_sql(sql).each do |row|
puts "#{row.vehicle_model}, " <<
"avg. age: #{row.average_age}, " <<
"avg. accidents: #{row.average_accident_count}"
end
The output would look something like this:
Ford Explorer, avg. age: 43.010, avg. accidents: 0.400
Honda CRX, avg. age: 18.720, avg. accidents: 1.250
. . .
CHAPTER 2 n CALCULATING STATISTICS WITH ACTIVE RECORD 21
Let??™s put this knowledge to use with a more complicated example.
Analyzing Data with Grouping and Aggregates
The examples in Chapter 1 involved Transmegtech Studios, a hypothetical game development
company. Now let??™s suppose Transmegtech Studios has merged with another small
studio, J. Lee Games, and they want to analyze their combined base of game testers.
Specifically, they want to answer two questions:
How many players are at each salary rate? Answering this question should give the
merged companies a way to get a handle on their total beta-testing expenditures.
This question can be answered with simple grouping in Active Record, so that will
be tackled first.


Pages:
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59