SEARCH
0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Prev | Current Page 104 | Next

Ola Bini

"Practical JRuby on Rails Web 2.0 Projects: Bringing Ruby on Rails to Java"

up
create_table :products do |t|
t.column :name, :string
t.column :description, :text
t.column :product_type_id, :integer
# This price will be in cents to make it easier
# It might be better to implement this using Decimal
t.column :price, :integer
end
end
def self.down
drop_table :products
end
end
As you can see, you just added entries for description, product_type_id, and price.
A comment is also in place to make it obvious why the price is an integer. After adding the
database information, you need to change some parts in the model definition. That file is
called app/models/product.rb. After the current changes it should look like this:
class Product < ActiveRecord::Base
has_and_belongs_to_many :product_categories
belongs_to :product_type
end
The directives simply say what it looks like they say. Having belongs_to means that for
every ProductType there are zero or more Products for it. Using has_and_belongs_to_many
requires us to have a join table for product_categories, and that??™s what we??™ll look at next.
CHAPTER 4 ?–  STORE ADMINISTRATION 43
Product Categories
As for the other model objects, you generate the base with the generator script:
jruby script/generate model ProductCategory
The migration code for product categories is slightly more involved than the other two,
because you need to do a fair number of things in it. First of all, a product category belongs
to a product type, and has a name. That means the part that defines the product_categories
table looks like this (in db/migrate/003_create_product_categories.


Pages:
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116