ActiveRecord Migrations

Flashcard 5 of 6

What simplification could you make to the following migration?

class AddAddressToUsers < ActiveRecord::Migration
  def up
    add_column :users, :address, :string
  end

  def down
    remove_column :users, :address
  end
end

In many (most) cases, Rails can determine the relevant down for a given migration automatically, and you can simply write the change method with the code that would have gone in up.

class AddAddressToUsers < ActiveRecord::Migration
  def change
    add_column :users, :address, :string
  end
end

Check out the [Rails Guide to Migrations section on the Change Method][] for the full list of automatically reversible migration methods.

[Rails Guide to Migrations section on the Change Method]: http://edgeguides.rubyonrails.org/active_record_migrations.html#using-the-change-method

Return to Flashcard Results