Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails generate Migration

People also ask

What does rails generate migration do?

Migrations are a feature of Active Record that allows you to evolve your database schema over time. Rather than write schema modifications in pure SQL, migrations allow you to use a Ruby DSL to describe changes to your tables.

What is the command to create migration in Ruby?

The way of creating migration by using the rails generates command in rails command prompt. First open rails command prompt in start>>Railsinstaller>>command prompt then root your application directory.

How do I create a migration file?

To create a new migration, you can run the make:migration Artisan command and that will bootstrap a new class on your Laravel application, in the database/migrations folder. This class will contain a default boilerplate code.


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

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

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


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

rails generate migration add_description_to_products

AddDescriptionToProducts < ActiveRecords:: Migration[v]
  def change
    add_column :products :description :string
    add_column :name_of_table :name_of_column :data_type
  end

run rake db:migrate and your schema.rb will be updated automatically