SoFunction
Updated on 2025-04-07

Some notes when migrating Ruby on Rails


Save it under version control.
Use rake db:scheme:load instead of rake db:migrate to initialize an empty database.
Use rake db:test:prepare to update the schema of the test database.

Avoid setting default data in tables. Use the model layer to replace it.

  def amount
   self[:amount] or 0
  end

However, the use of self[:attr_name] is considered quite common, and you can also consider using a more wordy (disputedly more readable) read_attribute instead:

    

def amount
   read_attribute(:amount) or 0
  end

When writing constructive migrations (adding tables or fields), use the new Rails 3.1 method to migrate - use the change method instead of the up and down methods.

  

 # Past Way  class AddNameToPerson < ActiveRecord::Migration
   def up
    add_column :persons, :name, :string
   end

   def down
    remove_column :person, :name
   end
  end

  # New ways of preference  class AddNameToPerson < ActiveRecord::Migration
   def change
    add_column :persons, :name, :string
   end
  end