featureBlend
featureBlend

Reputation: 687

When I run the rake:db migrate command I get an error "Uninitialized constant CreateArticles"

I created a model ruby script/generate model Article (simple enuff)

Here is the migration file create_articles.rb:

def self.up
  create_table :articles do |t|
    t.column :user_id, :integer
    t.column :title, :string
    t.column :synopsis, :text, :limit => 1000
    t.column :body, :text, :limit => 20000
    t.column :published, :boolean, :default => false
    t.column :created_at, :datetime
    t.column :updated_at, :datetime
    t.column :published_at, :datetime
    t.column :category_id, :integer
  end

def self.down
  drop_table :articles
 end
end

When I run the rake:db migrate command I receive an error rake aborted! "Uninitialized constant CreateArticles."

Does anyone know why this error keeps happening?

Upvotes: 57

Views: 32703

Answers (4)

stevec
stevec

Reputation: 52748

The top answer solved for me. Just leaving this here in case it helps.

Example

If your migration file is called

20210213040840_add_first_initial_only_to_users.rb

then the class name in your migration file should be

AddFirstInitialOnlyToUsers

Note: if the class name doesn't match, it will error even if the difference is just a lower case t instead of an upper case 'T' in 'To' - so be careful of that!

Upvotes: 4

dgsan
dgsan

Reputation: 285

It's possible to get the given error if your class names don't match inflections (like acronyms) from config/initializers/inflections.rb.

For example, if your inflections include:

ActiveSupport::Inflector.inflections(:en) do |inflect|
  inflect.acronym 'DOG'
end

then you might need to make sure the class in your migration is:

class CreateDOGHouses < ActiveRecord::Migration[5.0]

rather than:

class CreateDogHouses < ActiveRecord::Migration[5.0]

Not super common, but if you generate a migration or a model or something, and then add part of it to inflections afterwards it may happen. (The example here will cause NameError: uninitialized constant CreateDOGHouses if your class name is CreateDogHouses, at least with Rails 5.)

Upvotes: 10

123
123

Reputation: 8951

If you're getting this error and it's NOT because of the migration file name, there is another possible solution. Open the class directly in the migration like this:

class SomeClass < ActiveRecord::Base; end

It should now be possible to use SomeClass within the migration.

Upvotes: 3

thetacom
thetacom

Reputation: 1904

Be sure that your file name and class name say the same thing(except the class name is camel cased).The contents of your migration file should look something like this, simplified them a bit too:

#20090106022023_create_articles.rb
class CreateArticles < ActiveRecord::Migration   
  def self.up
    create_table :articles do |t|
      t.belongs_to :user, :category
      t.string :title
      t.text :synopsis, :limit => 1000
      t.text :body, :limit => 20000
      t.boolean :published, :default => false
      t.datetime :published_at
      t.timestamps
    end
  end

  def self.down
    drop_table :articles
  end
end

Upvotes: 120

Related Questions