add_index(table_name, column_name, options = {}) Ver: 1.0.0
From: ActiveRecord::ConnectionAdapters::SchemaStatements Version 1.0.0
Comments

Adds a new index to the table. column_name can be a single Symbol, or an Array of Symbols.

The index will be named after the table and the first column names, unless you pass +:name+ as an option.

Examples
Creating a simple index
 add_index(:suppliers, :name)

generates

 CREATE INDEX suppliers_name_index ON suppliers(name)
Creating a unique index
 add_index(:accounts, [:branch_id, :party_id], :unique => true)

generates

 CREATE UNIQUE INDEX accounts_branch_id_index ON accounts(branch_id, party_id)
Creating a named index
 add_index(:accounts, [:branch_id, :party_id], :unique => true, :name => 'by_branch_party')

generates

 CREATE UNIQUE INDEX by_branch_party ON accounts(branch_id, party_id)

Sourcecode
# File src/rails-1.0.0/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 169
      def add_index(table_name, column_name, options = {})
        index_name = "#{table_name}_#{Array(column_name).first}_index"

        if Hash === options # legacy support, since this param was a string
          index_type = options[:unique] ? "UNIQUE" : ""
          index_name = options[:name] || index_name
        else
          index_type = options
        end

        execute "CREATE #{index_type} INDEX #{index_name} ON #{table_name} (#{Array(column_name).join(", ")})"
      end
Add New Note User Added Notes