本文介绍了当我运行 rake:db migrate 命令时,我收到一个错误“未初始化的常量 CreateArticles";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个模型 ruby​​ 脚本/生成模型文章(简单的enuff)

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

这是迁移文件 create_articles.rb:

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

当我运行 rake:db migrate 命令时,我收到错误 rake aborted!未初始化的常量 CreateArticles."

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?

推荐答案

确保你的文件名和类名是一样的(除了类名是驼峰式的).你的迁移文件的内容应该看起来像这也简化了它们:

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

这篇关于当我运行 rake:db migrate 命令时,我收到一个错误“未初始化的常量 CreateArticles";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 20:51