Before you go any further, it??™s important
that you migrate the database, so all these new tables are available:
jruby -S rake db:migrate
jruby -S rake db:migrate RAILS_ENV=test
When this is done, you can edit the model files and add all relationships that until now
you only had in the database. You begin with Customer, in the file app/models/customer.rb.
It should look like this:
class Customer < ActiveRecord::Base
has_many :orders
def to_s
"#{given_name} #{sur_name}"
end
end
The only thing you would possibly want from a Customer is to know which orders he or
she is associated with. In some circumstances printing a Customer is interesting, so you add
a custom to_s method to cater for this. Next, open the file app/models/order.rb and change
it into this:
class Order < ActiveRecord::Base
has_many :order_lines
belongs_to :customer
end
An Order has many OrderLines and has one Customer. You can find the definitions for
OrderLine in app/models/order_line.rb and you should change them into this:
class OrderLine < ActiveRecord::Base
belongs_to :product
belongs_to :order
end
All this is obvious. Finally, the User model is good as it is.
User Administration
Now it??™s time to add a new controller. The purpose of this one is to allow us to add or remove
users, because you??™ll need this as soon as you switch on the authentication system. You??™ll
begin with a scaffold for this:
jruby script/generate scaffold User
Then you??™ll tear a whole lot of stuff out of it, because you just want to be able to do three
things: list the users, add a user, and remove a user.
Pages:
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139