Rails Forum
Rails Work - the best place to post and find great Ruby on Rails jobs.
Username
Password

You are not logged in.

New Posts in this thread

#1 2006-10-28 12:36:07

vin
Back in action!
From: South Florida
Registered: 2006-05-10
Posts: 842
Website

Rails Migrations for beginners

In the "old days" of Rails (before version 1.0), most people created the databases their apps use directly in SQL, or by using tools like MySQLFront to help them along. This approach has its problems though: how do you know what has changed in the data model between versions of your app? Why do we have to use SQL here when ActiveRecord does a great job of abstracting that away for us? The answer to this came in the form of ActiveRecord migrations. With migrations, you create your database schema in Ruby instead of SQL DDL. If you want to change your data model, say by renaming a column or adding a table, you just write another migration class to do it. Your database keeps track of versioning for you, and you can easily see what changed in the data model just by reading the migration code. It's great, and if you want to learn how to use migrations in your Rails apps, then read on.

How to use migrations:

First you use the Rails generator to create your migration. You can generate just the migration, or you can do it by generating a model, which will also give you a migration file.


Code :  bash - fold - unfold
  1. $ ./script/generate migration add_posts
This will create something like 001_add_posts.rb in the /db/migrate folder of your app.

Then you create the table like so:

Code :  ruby - fold - unfold
  1. class AddPosts < ActiveRecord::Migration
  2.  
  3. def self.up
  4.   create_table "posts" do |t|
  5.     t.column "title", :string
  6.     t.column "content", :text
  7.   end
  8. end
  9.  
  10. def self.down
  11.   drop_table "posts"
  12. end
  13.  
  14. end 
The self.up method runs when you run the database migration via rake. Self.down runs when you tear down the database.

If you need to update the table later (say, adding a user_id column to track which post belongs to a user), you would generate another migration to add the column:


Code :  bash - fold - unfold
  1. $ ./script/generate migration add_user_id_to_posts
Then you'd add your column in /db/migrate/002_add_user_id_to_posts.rb:

Code :  ruby - fold - unfold
  1. class AddUserIdToPosts < ActiveRecord::Migration
  2.  
  3. def self.up
  4.   add_column "posts", "user_id", :integer
  5.   #optional, but it could help depending on your site
  6.   add_index "posts", "user_id"
  7. end
  8.  
  9. def self.down
  10.  
  11. end
  12.  
  13. end 
When you want to actually generate your database (be sure your information for database.yml is correct!), run rake to migrate the database:

Code :  bash - fold - unfold
  1. $ RAILS_ENV=development rake db:migrate
(setting RAILS_ENV may not be necessary depending on how your environment is set up).

There you go! You have a database with all the table and column information you wrote in. Wasn't that easy?

Don't think of each migration file as a table, think of it as a change to the data model. You can create more than one table in each migration, or you can just add/remove columns, indices, etc.

Migrations act as version control for your database changes and they're extremely useful.


vinnie - rails forum admin

Offline

 

#2 2007-08-15 00:07:04

mc
Ticketholder
Registered: 2007-06-07
Posts: 9

Re: Rails Migrations for beginners

What if you add another column but want to switch the order that they display?

Offline

 

#3 2007-08-15 00:10:06

Adam
Administrator
From: UK
Registered: 2006-09-01
Posts: 690

Re: Rails Migrations for beginners

Display where? Your views should be made by you so you choose the order yourself. Do you in scaffolds?

Offline

 

#4 2007-11-30 20:16:13

mr_dizzy
Ticketholder
From: Isle of Wight, UK
Registered: 2007-04-03
Posts: 12
Website

Re: Rails Migrations for beginners

For beginners and experts alike, I've just finished a Rails Migrations cheatsheet which summarises everything on one page. It's a compact printable PDF file which should act as a handy reference.

http://dizzy.co.uk/articles/rails-migrations


http://dizzy.co.uk/portfolio/show - Making business beautiful.

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson