Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Use of Migration in Rails

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 172
    Comment on it

    Rails Migrations can be used to alter the database.Migration allows ruby to define the changes in database schema in a consistent and easy way.
    So If one deleloper make some changes in database schema, the other developers just need to update, and run "rake db:migrate".
    Using Migration we can create a new table, delete a table , add a new column in existing table and many more.
    Migration support all the basic data types with valid options

    Lets create a migration using the generate script.

    rails generate migration products

    It will create a file in db/migrate directory. The name of the file will be in the form of YYYYMMDDHHMMSS_create_products.rb

    Open the file and you will get something like this:

    class CreateProducts < ActiveRecord::Migration
     def change
      end
    end

     

    You can alter this file

    class CreateProducts < ActiveRecord::Migration
      def change
        create_table :products do |t|
          t.string :name
          t.text :description
     
          t.timestamps
        end
      end
    end

    Finally run the migration

    rake db:migrate

    Now you can see there is a table in your daabase named products.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: