Prev | Current Page 31 | Next

David Berube

"Practical Reporting with Ruby and Rails"

The fact that Active
Record is now aware of the table??™s name means that it can create methods to match the
field names and automatically generate SQL statements to interact with the database.
One of the methods you inherit from ActiveRecord::Base allows you to delete all of
the records from previous runs (of course, there won??™t be any the first time through):
Player.delete_all
CHAPTER 1 n DATA ACCESS FUNDAMENTALS 9
Next, you use the new method to add a record:
Player.new do |p|
p.name = "Matthew 'm_giff' Gifford"
p.salary = 89000.00
p.wins = 7
p.save
end
The new method has a few different forms. In this case, you??™re passing it a block, and it
passes a new Player object to your block. You could also use this form:
p = Player.new
p.name = "Matthew 'm_giff' Gifford"
p.salary = 89000.00
p.wins = 7
p.save
Alternatively, you could use this form:
p = Player.new(:name=> "Matthew 'm_giff' Gifford", :salary => 89000.00, :wins => 7)
p.save
All three of these forms are just variations that perform the same action.


Pages:
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
Sergio Mendes Kimberley Locke tanie linie lotnicze Łzy sita