up
create_table :librarians do |t|
t.column :name, :string
t.column :authentication_id, :integer
end
Librarian.create :name => 'Administrator',
:authentication => Authentication.find_by_username('admin')
end
def self.down
drop_table :librarians
end
end
You also added a name, for good measure, and also made sure that there is at least one
Librarian from the beginning, so you can create more of them later on. Notice that you need
to define both an Authentication model and a Librarian model within the migration, so that
you have access to them. You can??™t use the class created in CreateAuthentications, because
that one was confined to the scope of that class, and it??™s even possible that the Rails migration
engine tears down everything between different parts of the migration.
The Borrower migration is almost the same as the Librarian one, except that you don??™t
need to create any initial Borrower; it will be possible for Borrowers to register their own
accounts later on:
class CreateBorrowers < ActiveRecord::Migration
def self.up
create_table :borrowers do |t|
t.column :name, :string
t.column :email, :string
t.column :authentication_id, :integer
end
end
def self.down
drop_table :borrowers
end
end
Finally, here??™s the model that represents the borrowed books of a Borrower:
class CreateBorrowedBooks < ActiveRecord::Migration
def self.up
create_table :borrowed_books do |t|
t.column :book_description_id, :integer
t.column :library_id, :integer
CHAPTER 14 ?– THE LIBLIB RAILS APPLICATION 255
t.
Pages:
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380