# Problem With Migrations

**URL:** <https://www.ruby-forum.com/t/problem-with-migrations/145715>\
**Category:** Rails\
**Created:** [August 13, 2008, 8:36am UTC](https://www.ruby-forum.com/t/problem-with-migrations/145715 "2008-08-13T08:36:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pablo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ad7895/32.png) [@pablo](https://www.ruby-forum.com/u/pablo)\
**Post date:** [August 13, 2008, 8:36am UTC](https://www.ruby-forum.com/t/problem-with-migrations/145715/1 "2008-08-13T08:36:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pablo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ad7895/32.png) [@pablo](https://www.ruby-forum.com/u/pablo)\
**Post date:** [August 13, 2008, 4:47pm UTC](https://www.ruby-forum.com/t/problem-with-migrations/145715/2 "2008-08-13T16:47:33Z")

</div>

class\_name =\> ‘CreateProductCategories::ProductType’ did the trick!!!

Thank you so much Frederick!!

---

<div class="post-metadata">

**Author:** ![pablo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ad7895/32.png) [@pablo](https://www.ruby-forum.com/u/pablo)\
**Post date:** [August 13, 2008, 8:45am UTC](https://www.ruby-forum.com/t/problem-with-migrations/145715/3 "2008-08-13T08:45:28Z")

</div>

On 13 Aug 2008, at 00:45, Pablo F. wrote:

> 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:

If you run them one at a time does it work ? (ie move migrations 2 and  
3 out of the migrate folder for a minute, run db:migrate,move them  
back again, run miograte again?)

Seems related to [http://dev.rubyonrails.org/ticket/6272](http://dev.rubyonrails.org/ticket/6272)

in which case the problem is that the belongs\_to :product\_type is  
looking for the wrong ProductType class. You might be able to guide it  
on its way with class\_name =\> ‘CreateProductCategories::ProductType’

Fred

---

<div class="post-metadata">

**Author:** ![pablo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ad7895/32.png) [@pablo](https://www.ruby-forum.com/u/pablo)\
**Post date:** [August 13, 2008, 5:46pm UTC](https://www.ruby-forum.com/t/problem-with-migrations/145715/4 "2008-08-13T17:46:12Z")

</div>

On 13 Á×Ç, 18:46, Pablo F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class\_name =\> ‘CreateProductCategories::ProductType’ did the trick!!!
> 
> Thank you so much Frederick!!  
> I think this will work too

class ProductType \< ActiveRecord::Base; end  
class CreateProductTypes \< ActiveRecord::Migration  
…  
def self.load\_data  
ProductType.create :name =\> ‘Book’  
ProductType.create :name =\> ‘Music’  
ProductType.create :name =\> ‘Movie’  
end  
…  
end
