Rails generate Migration
Asked Answered
R

5

69

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
Repetend answered 1/3, 2013 at 16:14 Comment(3)
Have you looked at http://guides.rubyonrails.org/migrations.html#using-the-change-methodHellenism
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 :)Repetend
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.Corium
I
102

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

Integral answered 15/11, 2013 at 18:47 Comment(2)
for rails version below 3 rails generate migration command will be script/generate migration ...Safekeeping
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.Slur
M
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
Munos answered 1/3, 2013 at 20:19 Comment(0)
P
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

Principe answered 14/7, 2017 at 7:51 Comment(0)
H
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
Hellenism answered 1/3, 2013 at 16:41 Comment(0)
M
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

Microstructure answered 22/11, 2017 at 9:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.