This project is archived and is in readonly mode.

#1656 ✓invalid
Daniel Schierbeck

Add syntactic sugar for indexes in migrations

Reported by Daniel Schierbeck | December 28th, 2008 @ 06:16 PM | in 2.x

The current "sexy" syntax for creating and changing tables in migrations look very different from the way indexes are added and removed:

class CreateArticles < ActiveRecord::Migration
  def self.up
    create_table :articles do |t|
      t.string :title, :null => false
      t.text :body
      t.date :published_at
      t.references :author, :null => false
    end
    add_index :articles, :published_at  # not very DRY
  end
end

A more concise syntax can be achieved:

class CreateArticles < ActiveRecord::Migration
  def self.up
    create_table :articles do |t|
      t.string :title, :null => false
      t.text :body
      t.date :published_at
      t.references :author, :null => false
      t.index :published_at # <-- pretty dry!
    end
  end
end

A complementary option would be t.date :published_at, :index => true. This, however, ties the definition of the column with the addition of the index, which wouldn't be nice in change_table.

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Pages