The Hitchhiker’s Guide to Ruby On Rails Galaxy

Records of my voyage through RoR Galaxy

In Rails 2.0, use attribute pairs to create add and remove column migrations.

Posted by arjunghosh on February 14, 2008

We see a very useful feature in Rails 2.0 as defined in Changeset 7422, where now we can generate migration for adding and removing columns by directly passing attribute:type pairs to the migration generator.
Now for example, we need to add a column called ‘dept’ to students table where the model will be called ‘Student’.
Now previously we used something like:-

script/generate migration add_dept_to_student

Now in case of new better way of writing migration in Rails 2.0, we write it as follows:-

script/generate migration AddDeptToStudent dept:string

Here the migration name AddDeptToStudent holds the key. ‘Add’ specifies the we want to add column(s) and ‘Student’ separated by ‘To’ specifies the table.
Similarly, if we need to remove a column ‘dept’ :

script/generate migration RemoveDeptFromStudent dept:string

Similar to above, the migration name RemoveDeptFromStudent holds the key. ‘Remove’ specifies the we want to remove column(s) and ‘Student’ separated by ‘From’ specifies the table.

3 Responses to “In Rails 2.0, use attribute pairs to create add and remove column migrations.”

  1. vinhboy said

    Thanks for the interesting post.
    Today I was doing this

    “script/generate migration AddUserToClient user:references”

    It gave me this in the migration file

    “add_column :clients, :user, :references”

    However, when I run this migration I get a SQL syntax error.
    What am I doing wrong?

    Thanks.

  2. madhuka said

    Where is some screen spot support too..
    try this out

    http://madhukaudantha.blogspot.com/2010/01/ruby-on-rails-part-7-addingremoving.html

    Good post

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: