Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 当我运行rake:db migrate命令时,我得到一个错误";未初始化常量CreateArticles“;_Ruby On Rails_Rake_Rails Migrations - Fatal编程技术网

Ruby on rails 当我运行rake:db migrate命令时,我得到一个错误";未初始化常量CreateArticles“;

Ruby on rails 当我运行rake:db migrate命令时,我得到一个错误";未初始化常量CreateArticles“;,ruby-on-rails,rake,rails-migrations,Ruby On Rails,Rake,Rails Migrations,我创建了一个模型ruby脚本/生成模型文章(SimpleEnuff) 以下是迁移文件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

我创建了一个模型ruby脚本/生成模型文章(SimpleEnuff)

以下是迁移文件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中止!“未初始化的常量CreateArticles。”


有人知道为什么这个错误一直发生吗

确保您的文件名和类名是相同的(除了类名是驼峰大小写的)。迁移文件的内容应该类似于这样,也简化了一些:

#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

#20090106022023_create_articles.rb
类CreateArticles1000
t、 text:body,:limit=>20000
t、 布尔值:published,:default=>false
t、 日期时间:发布于
t、 时间戳
结束
结束
def自动关闭
投递表格:文章
结束
结束

如果您遇到此错误,而不是因为迁移文件名,则有另一种可能的解决方案。在迁移中直接打开类,如下所示:

class SomeClass < ActiveRecord::Base; end
classsomeclass

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

如果您的类名与
config/initializers/inductions.rb中的屈折变化(如首字母缩略词)不匹配,则可能会得到给定的错误

例如,如果您的屈折变化包括:

ActiveSupport::Inflector.inflections(:en) do |inflect|
  inflect.acronym 'DOG'
end
然后,您可能需要确保迁移中的类是:

class-CreateDOGHouses

而不是:

class-CreateDogHouses


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

我解决了最重要的问题。把这个留在这里以防万一

例子 如果调用了迁移文件

20210213040840_add_first_initial_only_to_users.rb
那么迁移文件中的类名应该是

AddFirstInitialOnlyToUsers

注意:如果类名不匹配,即使差异只是小写的
t
而不是“To”中的大写的“t”,它也会出错,所以要小心

你的迁移文件名是什么?你的类声明是什么样子的?20090106022023\u create\u articles.rb(迁移文件)^这不是和上面的(类声明)一样吗您的类声明应该包含上述所有内容,看起来像:class CreateMyModelRails g migration CreateArticles
,然后保持文件和类名不变。这对于更长和/或更复杂的类名很有帮助。。没有什么是完美的是的,这就是问题所在。虽然我有点失望,但rails在这方面限制太多了。是否有方法覆盖它,以便文件名和类名不需要匹配?我觉得被框架欺负了:-)