Prev | Current Page 36 | Next

David Berube

"Practical Reporting with Ruby and Rails"

sql. Then run the following MySQL command:
mysql -u your_mysql_username -p < player_schema_2.sql
Note that the SQL has the data already loaded into it (as specified with SQL INSERT
statements), so the script does not need to handle the data insertion directly.
Next, you need some code to analyze our data. The code shown in Listing 1-4 does
just that.
Listing 1-4. Analyzing Player Wins (player_wins.rb)
require 'active_record'
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.
:database => 'players_2')
class Player < ActiveRecord::Base
has_many :wins
def total_wins
total_wins = 0
self.wins.each do |win|
CHAPTER 1 n DATA ACCESS FUNDAMENTALS 13
total_wins = total_wins + win.quantity
end
total_wins
end
end
class Game < ActiveRecord::Base
has_many :wins
end
class Win < ActiveRecord::Base
belongs_to :game
belongs_to :player
end
games = Game.


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