Posted by
ihower
on
July 24, 2010
Always add index for foreign key, columns that need to be sorted, lookup fields and columns that are used in a GROUP BY. This can improve the performance for sql query. If you're not sure which column need to index , I recommend to use http://github.com/eladmeidar/rails_indexes, which provide rake tasks to find missing indexes.
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.

Comments
create_table "taggings", :force => true do |t| t.integer "tag_id" t.integer "taggable_id" t.string "taggable_type" t.integer "tagger_id" t.string "tagger_type" t.string "context" t.datetime "created_at" end add_index "taggings", ["tag_id"], :name => "index_taggings_on_tag_id" add_index "taggings", ["taggable_id", "taggable_type", "context"], :name => "index_taggings_on_taggable_id_and_taggable_type_and_context" add_index "taggings", ["tagger_id"], :name => "index_taggings_on_tagger_id" add_index "taggings", ["tagger_type"], :name => "index_taggings_on_tagger_type"terminal/rails_best_practices: using: Is that correct behavior for rails_best_practices?