Prev | Current Page 338 | Next

David Berube

"Practical Reporting with Ruby and Rails"


Listing 12-12. Create Grades Table Migration (db/migrate/002_create_grades.rb)
class CreateGrades < ActiveRecord::Migration
def self.up
create_table :grades do |t|
t.integer :student_id
t.integer :training_class_id
t.integer :percentage_grade
t.datetime :took_class_at
t.timestamps
end
end
def self.down
drop_table :grades
end
end
Listing 12-13 shows the code for the grade model.
Listing 12-13. Grade Model (app/models/grade.rb)
class Grade < ActiveRecord::Base
belongs_to :student
belongs_to :training_class
end
Save this as app/models/grade.rb.
Finally, create the last model, training_class:
ruby script/generate model training_class
exists app/models/
exists test/unit/
exists test/fixtures/
create app/models/training_class.rb
create test/unit/training_class_test.rb
create test/fixtures/training_class.yml
CHAPTER 12 n CREATING REPORTS WITH RUBY AND MICROSOFT OFFICE 244
exists db/migrate
create db/migrate/003_create_training_class.rb
The code for the third and last migration is shown in Listing 12-14.


Pages:
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
Maria McKee Linkin Park Anita Lipnicka Mandaryna Magma