ActiveRecord annoyance

July 24, 2010 by alex

class AnExampleWhichAnnoysMe < ActiveRecord::Migration
  def self.up
    create_table :test do |t|
      t.column :column_a, :integer
    end
    add_column :test, :column_b, :integer
    add_index :test, :column_b, :name=>'idx_on_test_col_b'
  end

  def self.down
    remove_index :test, :name=>'idx_on_test_col_b'
    remove_column :test, :column_b
    drop_table :test end
  end
  • Columns: add & remove.
  • Indexes: add & remove.
  • Tables: create & drop.

I wish these were more consistent. The names seem to be based on common SQL commands, but for that to be a useful rule-of-thumb, columns and indexes should be manipulated through some ‘alter’ method, since ALTER is usually the command you use to change these kinds of objects in SQL. In the context of ActiveRecord, I can see that add & drop really make more sense, but I wish that logic would have been applied to tables as well.

As it is, I often make the mistake of using create_column and drop_index.

☙ ☙ ☙