The Order model that you find in
db/migrate/005_create_orders.rb should look like this:
class CreateOrders < ActiveRecord::Migration
def self.up
create_table :orders do |t|
t.column :customer_id, :integer
t.column :time, :timestamp
t.column :status, :string
CHAPTER 4 ?– STORE ADMINISTRATION 61
end
end
def self.down
drop_table :orders
end
end
This is all you need to specify an order. You want to know the customer information, the
time the order happened, and if it has been handled or not. The file
db/migrate/006_create_order_lines.rb contains the database definitions for OrderLine:
class CreateOrderLines < ActiveRecord::Migration
def self.up
create_table :order_lines do |t|
t.column :order_id, :integer
t.column :product_id, :integer
t.column :amount, :integer
end
end
def self.down
drop_table :order_lines
end
end
An OrderLine is associated with an Order and a Product, and can contain more than one
of the same Product. Last, here??™s db/migrate/007_create_users.rb:
class CreateUsers < ActiveRecord::Migration
class User < ActiveRecord::Base; end
def self.up
create_table :users do |t|
t.column :username, :string
t.column :password, :string
end
User.create :username => 'admin',
:password => 'admin'
end
def self.down
drop_table :users
end
end
CHAPTER 4 ?– STORE ADMINISTRATION 62
The only thing that??™s different about this model is that you need to create at least one user
from scratch, which you can use to add further users.
Pages:
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138