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

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

Here is the create_articles.rb migration file:

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 get the rake aborted error! Msgstr "Uninitialized CreateArticles constant."

Does anyone know why this error continues?

+42
ruby-on-rails rake rails-migrations
Jan 05 '09 at 13:53
source share
3 answers

Make sure that your file name and class name say the same thing (except for the class name - with a camel). The contents of your migration file should look something like this: simplify them:

 #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 
+88
Jan 05 '09 at 14:33
source share

If you get this error, and it is NOT due to the name of the migration file, there is another possible solution. Open the class directly in the migration as follows:

 class SomeClass < ActiveRecord::Base; end 

Now you can use SomeClass as part of the migration.

+2
Jan 10 '17 at 19:55
source share

It is possible to get a given error if your class names do not correspond to bends (for example, abbreviations) from config/initializers/inflections.rb .

For example, if your flexes include:

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

you may need to make sure the class in your migration:

class CreateDOGHouses < ActiveRecord::Migration[5.0]

but not:

class CreateDOGHouses < ActiveRecord::Migration[5.0]

Not very ordinary, but if you create a migration or model or something else, and then add part of it to the excesses, this can happen. (The example here will result in NameError: uninitialized constant CreateDOGHouses if your class name is CreateDogHouses , at least with Rails 5.)

0
May 05 '17 at 10:58 p.m.
source share



All Articles