Hi, I’m following an example of a Rails Book (Practical JRuby on Rails
Projects, by Ola B.) and I’m stuck with a weird exception.
I have these 3 migration files, but when I run them (with rake
db:migrate) I see the following exception:
rake aborted!
CreateProductCategories is not missing constant ProductType!
Does anybody knows why? I append the 3 migrations below.
Thanks!
================================================
FILE 1:
class CreateProductTypes < ActiveRecord::Migration
class ProductType < ActiveRecord::Base; end
def self.up
create_table :product_types do |t|
t.column :name, :string
end
load_data
end
def self.load_data
ProductType.create :name => ‘Book’
ProductType.create :name => ‘Music’
ProductType.create :name => ‘Movie’
end
def self.down
drop_table :product_types
end
end
============================================
FILE 2
class CreateProducts < ActiveRecord::Migration
def self.up
create_table :products do |t|
t.column :name, :string
t.column :description, :text
t.column :product_type_id, :integer
t.column :price, :integer #in cents
end
end
def self.down
drop_table :products
end
end
===============================================
FILE 3
class CreateProductCategories < ActiveRecord::Migration
#Active record Class definitions.
class ProductType < ActiveRecord::Base; end
class ProductCategory < ActiveRecord::Base
belongs_to :product_type
end
def self.up
create_table :product_categories do |t|
t.column :product_type_id, :integer
t.column :name, :string
end
create_table :product_categories_products do |t|
t.column :product_id, :integer
t.column :product_category_id, :integer
end
load_data
end
def self.load_data
book = ProductType.find_by_name ‘Book’
music = ProductType.find_by_name ‘Music’
movie = ProductType.find_by_name ‘Movie’
%w(Computers Mysteries Crime).each do |c|
ProductCategory.create :product_type => book, :name => c
end
%w(Jazz Rock Metal Punk).each do |c|
ProductCategory.create :product_type => music, :name => c
end
%w(Action Comedy Thriller).each do |c|
ProductCategory.create :product_type => movie, :name => c
end
end
def self.down
drop_table :product_categories_products rescue nil
drop_table :product_categories
end
end