<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>PaulBarry.com - Adding Columns to Tables with the Migration Generator</title>
  <subtitle type="html">My thoughts, ideas, questions and concerns on technology, sports, music and life</subtitle>
  <id>tag:paulbarry.com,2007:Paulbarry.com</id>
  <generator uri="http://www.paulbarry.com" version="3.0">PaulBarry.com</generator>
  <link href="http://paulbarry.com/xml/atom/article/4893/feed.xml" rel="self" type="application/atom+xml"/>
  <link href="http://paulbarry.com/articles/2008/04/21/adding-columns-to-tables-with-the-migration-generator" rel="alternate" type="text/html"/>

  <updated>2008-11-07T04:22:14-05:00</updated>
  <entry>
    <author>
      <name>Paul Barry</name>
      <email>mail@paulbarry.com</email>
    </author>
    <id>urn:uuid:f834468e-1ae3-46de-9f26-ba5542983d71</id>

    <published>2008-04-21T12:11:46-04:00</published>
    <updated>2008-04-21T12:11:46-04:00</updated>
    <title type="html">Adding Columns to Tables with the Migration Generator</title>
    <link href="http://paulbarry.com/articles/2008/04/21/adding-columns-to-tables-with-the-migration-generator" rel="alternate" type="text/html"/>

    <category term="technology" scheme="http://paulbarry.com/articles/category/technology" label="Technology"/>
        <category term="Rails" scheme="http://paulbarry.com/articles/tag/rails"/>
    <category term="Migrations" scheme="http://paulbarry.com/articles/tag/migrations"/>
    <category term="Ruby" scheme="http://paulbarry.com/articles/tag/ruby"/>
        <summary type="html">&lt;p&gt;I just stumbled upon a neat little Rails feature and figured I&apos;d share with the world.  I&apos;m working on a Facebook application and I&apos;m going through some of the examples in &lt;a href=&quot;http://pragprog.com/titles/mmfacer&quot;&gt;Developing Facebook Platform Applications with Rails&lt;/a&gt;.  In chapter 4, one of the tasks is:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Let&apos;s add a boolean hit column to our attack model. I&apos;ll wait while you create that migration.   &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It doesn&apos;t tell you how to do that, which I like.  This is a simple enough task the reader should be able to do in their own, no need to clutter up the book with the code.&lt;/p&gt;

&lt;p&gt;So I set out to do that, but for whatever reason my mind is in a bit of a fog today.  I don&apos;t what it is, I&apos;ve been making an inordinate number of typos.  So when my fingers went to type the command to generate the migration, I just instinctively did this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;script/generate migration add_hit_to_attack hit:boolean
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I accidentally added the &lt;code&gt;hit:boolean&lt;/code&gt; column specification as if this were a &lt;code&gt;script/generate model Whatever&lt;/code&gt; command.  But it turns out Rails is able to read my mind.  I opened up the migration file that was generated and there was this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class AddHitToAttack &amp;lt; ActiveRecord::Migration
  def self.up
    add_column :attacks, :hit, :boolean
  end

  def self.down
    remove_column :attacks, :hit
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The code to add the column was already there.  I checked the docs and sure enough there is support for this naming convention:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;You can name your migration in either of these formats to generate add/remove
column lines from supplied attributes: AddColumnsToTable or RemoveColumnsFromTable

Example:
`./script/generate migration AddSslFlag`

With 4 existing migrations, this creates the AddSslFlag migration in
db/migrate/005_add_ssl_flag.rb

`./script/generate migration AddTitleBodyToPost title:string body:text published:boolean`

This will create the AddTitleBodyToPost in db/migrate/005_add_title_body_to_post.rb with
this in the Up migration:

  add_column :posts, :title, :string  
  add_column :posts, :body, :text  
  add_column :posts, :published, :boolean

And this in the Down migration:

  remove_column :posts, :published  
  remove_column :posts, :body  
  remove_column :posts, :title
&lt;/code&gt;&lt;/pre&gt;
</summary>
    <content type="html">&lt;p&gt;I just stumbled upon a neat little Rails feature and figured I&apos;d share with the world.  I&apos;m working on a Facebook application and I&apos;m going through some of the examples in &lt;a href=&quot;http://pragprog.com/titles/mmfacer&quot;&gt;Developing Facebook Platform Applications with Rails&lt;/a&gt;.  In chapter 4, one of the tasks is:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Let&apos;s add a boolean hit column to our attack model. I&apos;ll wait while you create that migration.   &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It doesn&apos;t tell you how to do that, which I like.  This is a simple enough task the reader should be able to do in their own, no need to clutter up the book with the code.&lt;/p&gt;

&lt;p&gt;So I set out to do that, but for whatever reason my mind is in a bit of a fog today.  I don&apos;t what it is, I&apos;ve been making an inordinate number of typos.  So when my fingers went to type the command to generate the migration, I just instinctively did this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;script/generate migration add_hit_to_attack hit:boolean
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I accidentally added the &lt;code&gt;hit:boolean&lt;/code&gt; column specification as if this were a &lt;code&gt;script/generate model Whatever&lt;/code&gt; command.  But it turns out Rails is able to read my mind.  I opened up the migration file that was generated and there was this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class AddHitToAttack &amp;lt; ActiveRecord::Migration
  def self.up
    add_column :attacks, :hit, :boolean
  end

  def self.down
    remove_column :attacks, :hit
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The code to add the column was already there.  I checked the docs and sure enough there is support for this naming convention:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;You can name your migration in either of these formats to generate add/remove
column lines from supplied attributes: AddColumnsToTable or RemoveColumnsFromTable

Example:
`./script/generate migration AddSslFlag`

With 4 existing migrations, this creates the AddSslFlag migration in
db/migrate/005_add_ssl_flag.rb

`./script/generate migration AddTitleBodyToPost title:string body:text published:boolean`

This will create the AddTitleBodyToPost in db/migrate/005_add_title_body_to_post.rb with
this in the Up migration:

  add_column :posts, :title, :string  
  add_column :posts, :body, :text  
  add_column :posts, :published, :boolean

And this in the Down migration:

  remove_column :posts, :published  
  remove_column :posts, :body  
  remove_column :posts, :title
&lt;/code&gt;&lt;/pre&gt;
</content>
  </entry>
  </feed>