68

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
3
  • 1
    Have you looked at http://guides.rubyonrails.org/migrations.html#using-the-change-method
    – hwatkins
    Mar 1, 2013 at 16:18
  • 8
    Yes I have read the manual, but some things are not clear to me. This is why I'm asking. I will appreciate a little guide step by step :) Mar 1, 2013 at 16:32
  • 1
    If you haven't migrate that migration, you can directly change to that migration file with let's say t.integer :product_type. My method while working on development was change directly to that migration even if after it migrated, and when it ready to be push, what I do just reset my db or re-create my db. That way make make migration file cleaner. Just share, hope can help.
    – ksugiarto
    Jun 22, 2013 at 4:41

5 Answers 5

101

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

Sources:

Creating a Migration

Running Migrations

2
  • 2
    for rails version below 3 rails generate migration command will be script/generate migration ... Aug 28, 2014 at 7:19
  • The default type for new columns is string, so you actually don't have to be explicit in the command unless it's a different type. Jun 15, 2015 at 17:54
20

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
4

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

0
2

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
2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.