there is two ways to add index to db table
1. references
if we have two tables , article and commens, the association is 1:M , in order to make search data fast , we should add index to the db table, when we use rails command line to generate the model, you can do it like this
rails g model artile title:string rails g model comment content:string artile:references
then open the generated migration, you'll see
class CreateComments < ActiveRecord::Migration def change create_table :comments do |t| t.string :content t.references :artile t.timestamps end add_index :comments, :artile_id end end
2. add index by yourself
remove the comment model , run rails d model comment, then do it like this
rails g model comment content:string article_id:integer:index
the result is the same with references