Ruby Forum Ruby on Rails > Problem With Migrations

Posted by Pablo Fernandez (Guest)
on 13.08.2008 08:36
(Received via mailing list)
Hi, I'm following an example of a Rails Book (Practical JRuby on Rails
Projects, by Ola Bini) 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
Posted by Frederick Cheung (Guest)
on 13.08.2008 08:45
(Received via mailing list)
On 13 Aug 2008, at 00:45, Pablo Fernandez wrote:

>
> Hi, I'm following an example of a Rails Book (Practical JRuby on Rails
> Projects, by Ola Bini) 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

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
Posted by Pablo Fernandez (Guest)
on 13.08.2008 16:47
(Received via mailing list)
class_name => 'CreateProductCategories::ProductType' did the trick!!!

Thank you so much Frederick!!
Posted by cheef (Guest)
on 13.08.2008 17:46
(Received via mailing list)
On 13 ΑΧΗ, 18:46, Pablo Fernandez <fernandezpabl...@gmail.com> 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