Prev | Current Page 34 | Next

David Berube

"Practical Reporting with Ruby and Rails"

rubydoc.
org/core/classes/Kernel.html#M005962.
This example was fairly simple, but you can see how trivial it is to do data manipulations
with Active Record.
Calculating Player Wins
Now suppose that the new game release was a success, and Transmegtech has hired professional
game players to beta test all of the company??™s games. Your boss now wants you
to calculate which player has the highest wins for each individual title, as well as which
player has the most wins overall.
For this report, you??™ll need more than one table. Fortunately, Active Record has a
rich set of associations that describe the relationships between tables: the has_many
relationship describes a one-to-many relationship, the has_one association describes a
one-to-one relationship, and so forth. Those relationships are created inside your model
definitions. Once you??™ve created them, you get a number of helper methods for free.
A method named after the association is added to the class, which can be enumerated,
inserted into, and so forth.


Pages:
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46