stack twitter rss linkedin cross

Wilco van Esch

Skip to main content

Search results

    Doing an Active Record migration in Rails

    To change your Rails models, you should create a migration containing the change and execute the migration.

    First, generate the migration. From within your app directory:

    rails g migration DESCRIPTION
    

    Edit the file, which has been created in db/migrate

    Define your desired changes. You can place them in a function change (with reversible if you want to be able to undo the change), or place them in functions up and down, where up defines what should be changed and down defines how to return to the previous state if you decide to roll back this migration.

    If you want to make the change as a SQL query, you can use:

    execute <<-SQL
     YOUR SQL QUERY
    SQL
    

    Then on the command line run:

    (OS X Terminal) rake db:migrate

    (Windows CMD) ruby bin/rake db:migrate (Heroku CLI) heroku run rake db:migrate

    More information can be found on the Active Record Migrations section of the RailsGuides site.