Prev | Current Page 28 | Next

David Berube

"Practical Reporting with Ruby and Rails"

rb)
require 'active_record'
ActiveRecord::Base.establish_connection(
:adapter => 'mysql',
:host => 'localhost',
:username => 'your_mysql_username_goes_here',
:password => 'your_mysql_password_goes_here' :database => 'players')
class Player < ActiveRecord::Base
end
Player.delete_all
Player.new do |p|
p.name = "Matthew 'm_giff' Gifford"
p.salary = 89000.00
p.wins = 11
p.save
end
Player.new do |p|
p.name = "Matthew 'Iron Helix' Bouley"
p.salary = 75000.00
p.wins = 4
p.save
end
CHAPTER 1 n DATA ACCESS FUNDAMENTALS 7
Player.new do |p|
p.name = "Luke 'Cable Boy' Bouley"
p.salary = 75000.50
p.wins = 7
p.save
end
salary_total = 0
win_total=0
players = Player.find(:all)
players.each do |player|
puts "#{player.name}: $#{'%0.2f' % (player.salary/player.wins)} per win"
salary_total = salary_total + player.salary
win_total = win_total + player.wins
end
puts "\nAverage Cost Per Win : $#{'%0.2f' % (salary_total / win_total )}"
nNote If you connect to MySQL via Unix sockets, and it??™s in a nonstandard location, you can add a
:socket=>'path/to/your/socket' option to the ActiveRecord::Base.


Pages:
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
wróżby miłosne cukiernia-stykowscy.pl salon kosmetyczny kraków pompy ciepła Kalkulator kredytowy