Rails generate Migration
Ruby on-Rails-3.1Ruby on-Rails-3.1 Problem Overview
I currently have migration called Products, and I simply want to add few more strings to this migration such as description and product type. What is the best way to do this?
class CreateProducts < ActiveRecord::Migration
def change
create_table :products do |t|
t.string :name
t.decimal :price
t.text :description
t.timestamps
end
end
end
Ruby on-Rails-3.1 Solutions
Solution 1 - Ruby on-Rails-3.1
Just run
rails g migration add_description_to_products description:string
rails g migration add_product_type_to_products product_type:string
and then run
rake db:migrate
Solution 2 - Ruby on-Rails-3.1
In the development of any practical application, you will be doing quite a few migrations which are basically DDL (data definition language) statements. In real life, you will have several environments (development, test, production, etc.) and it is highly possible that you will be changing the development database while you have a version in production. For this reason, the Rails way is to generate a new migration for any changes to the database instead of making direct changes to existing migration file.
So, familiarize yourself with migrations.
For the specific question, you can do:
rails g migration add_attributes_to_products attr1 attr2 attr3
This will generate a new migration file for adding 3 new attributes to products table (to the Product model). The default type for the attributes is string
. For others, you have specify it like:
rails g migration add_attributes_to_products attr1:integer attr2:text attr3:enum
Solution 3 - Ruby on-Rails-3.1
use rollback
if your last action is migration
rake db:rollback
Then add attributes in the migration file
class CreateProducts < ActiveRecord::Migration
def change
create_table :products do |t|
t.string :name
t.decimal :price
t.text :description
t.string :product_type #adding product_type attribute to the products table
t.timestamps
end
end
end
After that migrate using
rake db:migrate
If migrate is not your last action, generate a new migration file as of in the above answers
rails g migration add_attributes_to_products product_type:string
The above code only generates the migration file but you want to use rake db:migrate
to migrate the file.
If you want to do any more changes to that migration file such as adding more atributes do it before migrating otherwise you have to use the method I mentioned in the beginning if your last action is migration or else you need to generate another migration file. check this link to know more about migration http://guides.rubyonrails.org/v3.2.8/migrations.html
Solution 4 - Ruby on-Rails-3.1
Assuming you created the table with the migration above, then to add product_type (you already had description) you would do:
# db/migrate/20130201121110_add_product_type_to_product.rb
class AddProductTypeToProduct < ActiveRecord::Migration
def change
add_column :products, :product_type, :string
Product.all.each do |product|
product.update_attributes!(:product_type => 'unknown')
end
end
end
Solution 5 - Ruby on-Rails-3.1
Running following command
rails generate migration add_description_to_products
would generate following file
AddDescriptionToProducts < ActiveRecords:: Migration[v]
def change
add_column :products :description :string
add_column :name_of_table :name_of_column :data_type
end
Later run
rake db:migrate
and your schema.rb will be updated automatically