Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 865
featureBlend Avatar asked Jan 05 '09 13:01

featureBlend


People also ask

How do you run down migration in Rails?

To run a specific migration up or down, use db:migrate:up or db:migrate:down . The version number in the above commands is the numeric prefix in the migration's filename. For example, to migrate to the migration 20160515085959_add_name_to_users. rb , you would use 20160515085959 as the version number.

How do you run migration in Ruby on Rails?

Active Record tracks which migrations have already been run so all you have to do is update your source and run rake db:migrate. Active Record will work out which migrations should be run. It will also update your db/schema. rb file to match the structure of your database.

What does db Migrate do?

A migration means that you move from the current version to a newer version (as is said in the first answer). Using rake db:migrate you can apply any new changes to your schema. But if you want to rollback to a previous migration you can use rake db:rollback to nullify your new changes if they are incorrectly defined.

Where are migrations stored Rails?

Every Rails app has a special directory— db/migrate —where all migrations are stored. Let's start with a migration that creates the table events into our database. This command generates a timestamped file 20200405103635_create_events. rb in the db/migrate directory.


2 Answers

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 
like image 105
thetacom Avatar answered Oct 16 '22 22:10

thetacom


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

like image 24
dgsan Avatar answered Oct 16 '22 21:10

dgsan