Prev | Current Page 45 | Next

David Berube

"Practical Reporting with Ruby and Rails"


nNote The calculate form takes the abbreviated version of the function name, such as avg for
average. However, the shortcut form takes a longer version. For example, you could use either
Person.calculate(:avg, :age) or Person.average(:age). This is confusing, but the idea is that the
calculate form passes your function directly to your database, so you can use any statistical function
defined in your database, whereas the convenience functions are fixed, so they can have easier to understand
names.
You can also combine grouping and aggregate functions. For example, if you wanted
to print the average accident count for each age, you could do so as follows:
Person.calculate(:avg, :accident_count, :group=>'age').each do |player|
age, accident_count_average = *player
puts "Average Accident Count #{'%0.3f' % accident_count_average} for age #{age}"
end
CHAPTER 2 n CALCULATING STATISTICS WITH ACTIVE RECORD 20
Note that the object passed to the block is an array. In the array, the grouped field
comes first, followed by the calculated field.


Pages:
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
teksty pl mapa Margo Mattafix Sophie Milman mapa muzykowo