55

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?

hora
  • 3,661
  • 5
  • 25
  • 26
featureBlend
  • 667
  • 1
  • 8
  • 10
  • What's the name of your migration file and what does your class declaration look like? – thetacom Jan 05 '09 at 14:14
  • 20090106022023_create_articles.rb (migration file) ^ Wouldn't that be the same as above (class declaration) – featureBlend Jan 05 '09 at 14:22
  • Your class declaration should enclose all of the above and looks something like: class CreateMyModel < ActiveRecord::Migration – thetacom Jan 05 '09 at 14:26
  • If the answer resolves the issue, please mark it as accepted. – Victor May 08 '13 at 08:12
  • 1
    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 May 11 '15 at 20:33

4 Answers4

119

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
thetacom
  • 1,894
  • 1
  • 10
  • 15
  • Sublime doesn't have a refactor.. nothing is perfect – McSas Nov 01 '12 at 05:13
  • 1
    Yep, that's the problem. Though I'm a little disappointed rails is so restrictive in this. Is there a way to override it so that the filename and the class name does not need to match? I feel bullied by the framework :-) – Renra Jul 03 '14 at 07:14
10

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

dgsan
  • 275
  • 2
  • 9
3

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.

123
  • 8,733
  • 14
  • 57
  • 99
3

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!

stevec
  • 41,291
  • 27
  • 223
  • 311