Generating migrations gets easier – Rails 2.0 a feature a day #6

In: Open Source|Ruby on Rails|Web development

24 Dec 2007

You may not have noticed this yet, but Rails 2.0 has a new convenient syntax for generating ActiveRecord migrations. Go ahead, run the migration generator with script/generate migration. I’ll wait.

Yup, you can now specify the columns you want to add in your migration by passing attribute/type pairs to the migration generator. Ergo,

script/generate migration AddMoreToAnime episodes:integer licensed:boolean

will generate a migration like so:

class AddMoreToAnime < ActiveRecord::Migration
  def self.up
    add_column :animes, :episodes, :integer
    add_column :animes, :licensed, :boolean

  def self.down
    remove_column :animes, :licensed
    remove_column :animes, :episodes

This ties in pretty nicely with the syntax for the model generator (script/generate model), which also accepts attribute pairs. Tiny change but “it’s all about consistency”.

For the geeks among you, check out the related changeset and ticket.

About the contributor, Pratik Naik

Pratik Naik (WorkingWithRails profile), probably better known as lifofifo or just lifo, is an active Rails contributor and can be found on #rails-contrib and #rubyonrails nearly 24/7. Almost everyone who’s used Rails has used code that lifo has written. Oh, and lifo also won one of the monthly Rails Hackfests.

He also keeps a pretty good blog where he posts mostly on Rails hacking and tips.

2 Responses to Generating migrations gets easier – Rails 2.0 a feature a day #6



December 24th, 2007 at 5pm

Don’t forget the –svn at the end to automatically add to SVN!

I really like this new change – finally generating the whole, complete migration and adding to SVN is just as fast, if not faster, than just making the change directly in the DB – meaning it’s fast enough that I’ll actually use migrations. Fantastic work from Pratik and the Rails team.

Definitely one of my favourite features from 2.0!


Tecker.LOG » Blog Archive » Generating migrations gets easier?????Rails 2.0?? #6(??)

June 4th, 2008 at 7pm

[…] ??? Generating migrations gets easier – Rails 2.0 a feature a day #6 […]