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

Ruby on-RailsRakeRails Migrations

Ruby on-Rails Problem Overview


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?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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

Solution 2 - Ruby on-Rails

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.)

Solution 3 - Ruby on-Rails

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.

Solution 4 - Ruby on-Rails

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!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionfeatureBlendView Question on Stackoverflow
Solution 1 - Ruby on-RailsthetacomView Answer on Stackoverflow
Solution 2 - Ruby on-RailsdgsanView Answer on Stackoverflow
Solution 3 - Ruby on-Rails123View Answer on Stackoverflow
Solution 4 - Ruby on-RailsstevecView Answer on Stackoverflow