Always add DB index
24 Jul 2010
Bad Smell
class CreateComments < ActiveRecord::Migration
def self.up
create_table "comments" do |t|
t.string :content
t.integer :post_id
t.integer :user_id
end
end
def self.down
drop_table "comments"
end
end
By default, rails does not add indexes automatically for foreign key, you should add indexes by yourself.
Refactor
class CreateComments < ActiveRecord::Migration
def self.up
create_table "comments" do |t|
t.string :content
t.integer :post_id
t.integer :user_id
end
add_index :comments, :post_id
add_index :comments, :user_id
end
def self.down
drop_table "comments"
end
end
This is a basic practice, follow it.
Tags migration