• RSS
  • Delicious
  • Digg
  • Facebook
  • Twitter
  • Linkedin
  • Youtube

Migration: Adding/Removing columns are now much easier

You may have noticed by now, that in Rails 2.0 changeset 7422, you can specify columns you want to add/remove in your migration by passing attribute:type pairs to the migration generator.

For example, lets assume that we need to add a column ‘role’ in users table(User model). In this case generate a migration like:

script/generate migration AddRoleToUser role:string

Output:

class AddRoleToUser < ActiveRecord::Migration
  def self.up
    add_column :users, :role, :string
  end

  def self.down
    remove_column :users, :role
  end
end

Here AddRoleToUser plays the main role. ‘Add’ specifies the we want to add column(s) and ‘User’ separated by ‘To’ specifies the table.

Similarly, if we need to remove a column ‘role’ :

 script/generate migration RemoveRoleFromUser role:string

Output:

class RemoveRoleFromUser < ActiveRecord::Migration
  def self.up
    remove_column :users, :role
  end

  def self.down
    add_column :users, :role, :string
  end
end

Here RemoveRoleFromUser plays the main role. ‘Remove’ specifies the we want to remove column(s) and ‘User’ separated by ‘From’ specifies the table.

Isn’t it cool?

11 Responses so far.

  1. Arik Jones says:

    The auto_migration plugin already does this. But instead you edit one file (schema.rb). Good extraction though.

  2. Cesoure says:

    Подскажите шооблончег под WordPress 2.6.2, чтобы был похож на ваш webonrails.com.

    Заранее благодарю)

  3. radu says:

    thx dude.you saved my life!!Keep on the good work

  4. ravi says:

    Thanx a lot

  5. Razvan says:

    Hello,

    I am a beginner in rails, and I have a small question: where do I need to add “script/generate migration AddFnameToUser fname:string” in terminal or where.I work on NetBeans on ActveAdmin and I need to add some extra columns.

    Thx

    • Razvan says:

      LE: Never mind I figure it out :all I have to do is to put just generate not script/generate.But why ???

    • asvisri says:

      you have to give it in terminal as script/generate migration AddFnameToUser fname:string

      you will get like this
      create db/migrate/20120702084246_addFname_to_user.rb


Asset Pipeline has been extracted a...

The asset pipeline which was introduced in Rails 3.1, has ...

ClientSideValidations: Add/Map mode...

#/status/256699237230845952

Action and Page caching has been ex...

With the commit c82cf81f00f Action and Page caching has been extracted ...

turbo-sprockets-rails3: Speeds up ...

#/status/253420357841723393

EdgeRails: ActiveRecord::SessionSto...

With the commit 3324e28804 ActiveRecord::SessionStore is extracted out of Rails into ...

Asset Pipeline has been extracted a...

The asset pipeline which was introduced in Rails 3.1, has ...

ClientSideValidations: Add/Map mode...

#/status/256699237230845952

Action and Page caching has been ex...

With the commit c82cf81f00f Action and Page caching has been extracted ...

turbo-sprockets-rails3: Speeds up ...

#/status/253420357841723393

EdgeRails: ActiveRecord::SessionSto...

With the commit 3324e28804 ActiveRecord::SessionStore is extracted out of Rails into ...