This is an unforgettable mistake i made today while running some migrations in Rails.
Here is the first migration file. It was generated by using this simple command:
ruby script/generate model Articles
Simple enough right? The output of the generation was:
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 :categories
end
end
When i went ahead and tried the rake command to migrate to my database: rake db:migrate I kept getting an error “Uninitialized constant Create Articles” Rake Aborted!!
Having forgot that everything in Rails is Object Orientated (extends from one base class or another) i overlooked two things.
- I did not declare the class and what base it extended
- I did not add the schema file name into my migration file
Usually Rails generates the schema and adds the base class (Active Record) but this time it didn’t. Anyhow the fix was easy enough…
#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
So this is a classic mistake of us all having our moments..Thanks again to the users over at Stack Overflow for helping out. Their are some excellent trouble shooters out there.