Ruby-on-rails 当我运行 rake:db migrate 命令时,出现错误“未初始化的常量 CreateArticles”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/413150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 20:52:53  来源:igfitidea点击:

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

ruby-on-railsrakerails-migrations

提问by featureBlend

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

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

Here is the migration file create_articles.rb:

这是迁移文件 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."

当我运行 rake:db migrate 命令时,我收到一个错误 rake aborted!“未初始化的常量 CreateArticles。”

Does anyone know why this error keeps happening?

有谁知道为什么这个错误不断发生?

回答by thetacom

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

回答by 123

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 SomeClasswithin the migration.

现在应该可以SomeClass在迁移中使用了。

回答by dgsan

It's possible to get the given error if your class names don't match inflections (like acronyms) from config/initializers/inflections.rb.

如果您的类名与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]

class CreateDOGHouses < ActiveRecord::Migration[5.0]

rather than:

而不是:

class CreateDogHouses < ActiveRecord::Migration[5.0]

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 CreateDOGHousesif your class name is CreateDogHouses, at least with Rails 5.)

不是很常见,但是如果您生成迁移或模型或其他东西,然后将其中的一部分添加到变形中,则可能会发生。(这里的示例将导致NameError: uninitialized constant CreateDOGHouses如果您的类名是CreateDogHouses,至少在 Rails 5 中是这样。)