Skip to main content

Rails and Database Migrations

Posted by bleonard on April 5, 2007 at 2:58 PM PDT

After my first Ruby blog entry, David Rupp commented that I should do certain things the more conventional way, like using database migrations rather then SQL scripts. So here's a reprise of that entry, doing just that plus a couple of other minor improvements.

Setting Things Up

This time around I'm going to use Instant Rails. All of the instructions for setting up Instant Rails and NetBeans are documented here. However, these steps also work with the default JRuby configuration that comes with the Ruby module.

If you already have the Ruby module installed, check the Update Center for a newer version as it's updated almost daily. At the time of this writing it's up to version 0.54:




 

Create the Ruby Project

  1. Create a new Ruby On Rails Application named BlogDemo. Wait while the project is generated and the Mongrel Web Server is started.



Create a Database

  1. Right-click your mysql cookbook connection (if you don't have one, refer to here) and select Execute command. Enter:




  2. Right-click the MySQL (Connector/J driver) and select Connect Using. Enter jdbc:mysql://localhost/BlogDemo_development as the Database URL and root as the User Name.

Create the Model

  1. Switch back to the Projects tab, right-click the Models folder and generate a new model named Post. A migration is created for you as part of the model generation:






  2. Open 001_create_posts.rb and add the title column to the up method as follows:






  3. Right-click the BlogDemo project and select Run Rake Target > db > migrate.






  4. Switch to the Runtime tab to see the generated posts table and associated schema_info table.




Using a Scaffold

One of Rails' strengths is how easy it is to test your models. Check this out:

  1. Right-click the Controllers folder and generate a controller named Blog.


  2. Add the following to BlogController, which will provide a basic CRUD application around our model:






  3. Open routes.rb found in the Configuration folder and add the following:






  4. Delete index.html from the Public folder. Now our blog will load automatically when we run the application.


  5. Press F6 to start the Mogrel server and launch the browser.












Migrating Forward

  1. Right-click the Database Migrations folder and select Generate. Name the migration AddBody. Ruby will create a versioned migration script.






  2. Open 002_add_body.rb and modify it as follows:






  3. Right-click the BlogDemo project and select Run Rake Target > db > migrate.






  4. Return to the browser and click on the Edit link to see how Ruby has recognized our new body field.




  5. Note, support for migrating to a specific version is still being worked on. You can track the progress of that issue here. For now, you can do it from the command line. For example, to migrate back to version 1 of the schema, open the command prompt at the project's root and run rake db:migrate version=1







    If you do this, don't forget to migrate back to version 2 before continuing:






  6. Add a few more entries, for example:




Make the List Look More Like a Blog

This far, scaffolding has created a basic CRUD application that allows us to easily test our model. Here we will generate scaffolding that we can then use as a base for modifying our user interface.

  1. Right-click the Views folder and generate a scaffold referring to Post model and the Blog controller.

  2. Open list.rhtml and delete the entire table.
  3. In it's place, add the following:






  4. Save list.rhtml and refresh your browser.






  5. To display more like a typical blog, reverse the sort order by adding .reverse to the end of @posts in list.rhtml:










The Completed Application

BlogDemo.zip