Its all about Ruby On Rails
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?
| Print article | This entry was posted by Akhil Bansal on January 23, 2008 at 10:54 pm, and is filed under ROR, Rails, Rubyonrails, migration, ruby. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
about 2 years ago
The auto_migration plugin already does this. But instead you edit one file (schema.rb). Good extraction though.
about 1 year ago
Da nu Nahn
about 1 year ago
Подскажите шооблончег под WordPress 2.6.2, чтобы был похож на ваш webonrails.com.
Заранее благодарю)
about 1 year ago
Cool
about 9 months ago
thx dude.you saved my life!!Keep on the good work
about 7 months ago
Where is some screen spot support too..
http://madhukaudantha.blogspot.com/2010/01/ruby-on-rails-part-7-addingremoving.html
Good post
about 2 months ago
Thanx a lot