Home arrow Ruby-on-Rails arrow Page 4 - Author Management for an Online Bookstore

Author Management for an Online Bookstore

In a recent series of articles, you learned how to set up a proof of concept for an online bookstore created using Ruby-on-Rails. In this four-part article series, you'll start creating the application for real, using test-driven development. This article is excerpted from chapter two of the book Practical Rails Projects, written by Eldon Alameda (Apress; ISBN: 1590597818).

Author Info:
By: Apress Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 1
May 07, 2010
  1. · Author Management for an Online Bookstore
  2. · Testing in Rails
  3. · Creating the ActiveRecord Model
  4. · Editing the Migration File

print this article

Author Management for an Online Bookstore - Editing the Migration File
(Page 4 of 4 )

The next step is to open the migration file in a text editor and edit it so that the database will look how we want it to look. In this case, it means adding two fields to the table skeleton:

class CreateAuthors < ActiveRecord::Migration
  def self.up
    create_table :authors do |t|
t.column :first_name, :string
      t.column :last_name, :string

  def self.down
    drop_table :authors

Rails migrations use a special domain-specific language (DSL) written in Ruby. This way, the migration code is database-agnostic and can be used to deploy the application to different database platforms.

Every migration should implement the methodsself.upandself.down, which are created automatically when a new migration is generated by running either thegenerate modelor the explicitgenerate migrationcommand. Code insideself.upis run when the database is migrated to a higher version, and the code inself.downis run when the database is migrated back to an earlier version. In our first migration, we create a new table calledauthors and add columns for both the first and last names of an author. In theself.downpart, we drop theauthors table, just in case we someday would want to go back to ground zero.

Note  Even though every table used by ActiveRecord (except for join tables) should have a primary key field calledid, we didnít specify the creation of that column in our migration. This is because Rails will automatically create that field for every table, unless you explicitly tell it not to by using the:id => falseoption withcreate_table.

The commands in the migrations DSL mostly match the corresponding SQL statements. The following are the most commonly used commands. For complete documentation, see the Rails API documentation for migrations athttp://api.rubyonrails.org/classes/ActiveRecord/ Migration.html.

  1. create_table(table_name, options): Creates a new tabletable_name. If it is given a block parameter (as in theCreateAuthorsmigration shown in the previous section), the commands inside the block will be executed inside the scope of this table.
  2. column(column_name, type, options): Creates a new columncolumn_nameof typetypein the scope of enclosingcreate_tableblock. 
  3. add_column(table_name, column_name, type, options): Equivalent tocolumn, with the distinction that it is not used inside acreate_tableblock, and thus needs the name of the table as an argument. 
  4. drop_table(table_name): Drops the tabletable_namefrom the database. 
  5. remove_column(table_name, column_name): Removes columncolumn_namefrom tabletable_name.

Please check back for the next part of the series.

DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus

- Ruby-on-Rails Faces Second Security Flaw in ...
- Ruby 2.0 Prepped for February 2013 Release
- Why LinkedIn Switched from Ruby on Rails
- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials