53
votes

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?

4
What's the name of your migration file and what does your class declaration look like?thetacom
20090106022023_create_articles.rb (migration file) ^ Wouldn't that be the same as above (class declaration)featureBlend
Your class declaration should enclose all of the above and looks something like: class CreateMyModel < ActiveRecord::Migrationthetacom
If the answer resolves the issue, please mark it as accepted.Victor
To avoid this from happening, you can have Rails generate the migration file and Class name with one command: rails g migration CreateArticles and then keep the file and class name as is. This can be helpful with longer and/or more complex class names.aaron-coding

4 Answers

115
votes

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

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

3
votes

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.

2
votes

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!