55

モデルルビースクリプトを作成しました/モデル記事を生成します(単純なenuff)

移行ファイル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:dbmigrateコマンドを実行すると、rakeabortedというエラーが表示されます。「初期化されていない定数CreateArticles。」

このエラーが発生し続ける理由を誰かが知っていますか?

4

4 に答える 4

118

ファイル名とクラス名が同じであることを確認してください (クラス名がキャメルケースであることを除いて)。移行ファイルの内容は次のようになり、少し簡略化されます。

#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
于 2009-01-05T14:33:44.640 に答える
3

このエラーが発生し、移行ファイル名が原因ではない場合は、別の解決策が考えられます。次のように、移行でクラスを直接開きます。

class SomeClass < ActiveRecord::Base; end

SomeClass移行内で使用できるようになりました。

于 2017-01-10T19:55:33.073 に答える