Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Working with migration in Rails

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 412
    Comment on it

    Rails make use of ORM(Object-Relational Mapping) i.e a layer between your actual database and your model logic in the form of Active Record. As rails provide you with ORM in form of Active Record all the code that you need to write to interact with database , it is all written in a form of ruby files. These ruby files which keep track of our entire database from scratch i.e from creating a model with a respective table to adding or deleting any table or even removing or adding a new column to a particular table are called migration.

    The command that we use to create migration in rails is rails generate , which can be used in the following form depending on our use:

    1) rails generate model Student

    2) rails g model Student

    3) rails g model Student first_name last_name age:integer

    First 2 commands are exactly same with just a little difference of rails g which is a shortcut command command rails generate , while the 3rd command provide rails with an extra information regarding table column.We're saying we want this model to have three fields: first_name, last_name, and age. For first_name and last_name, we don't specify a type, so it defaults to a string. For age, we say that it should be an integer.

    The above command generate rails migration file in following pattern:

    create db/migrate/20130213204626_create_student.rb

    create app/models/student.rb

    The last command generate migration file in the following pattern:

    class CreateStudent < ActiveRecord::Migration
            def change
                create_table :student do |t|
                    t.string :first_name
                    t.string :last_name
                    t.integer :age
                    t.timestamps
            end
        end
    end 

    In the above file you can see the list of columns with their respective data type.If we don't add any data type to any column in create migration file it is taken as string by default. There is an additional column in migration file on name t.timestamps which creates two more columns in our table: created_at and updated_at. You don't need to mention table id in migration file as it is taken by default by rails as a primary key.

    Creating tables is just one use for a migration class; they're your method of tweaking the database, remember, so they can do any database job you might ever need do. But one of the important ideas with migrations is that you can roll them back, or undo their effects.

    To undo a last run migration just run the following command : rake db:rollback . Developer can even roll back his migration to a particular version by mentioning the exact version of migration to which he wants to roll back.

    Besides create_table and add_column, there are a bunch of other methods that you can use in your migration files.

    1) add_column

    2) add_index

    3) add_timestamps

    4) change_column

    5) change_table

    6) create_table

    7) drop_table

    8) remove_column

    9) remove_index

    10) remove_timestamps

    11) rename_column

    12) rename_index

    13) rename_table

    List of the supported types that you can use in your migration classes:

    1) binary

    2) boolean

    3) date

    4) datetime

    5) decimal

    6) float

    7) integer

    8) primary_key

    9) string

    10) text

    11) time

    12) timestamp

    For in-depth knowledge about migration please refer to following link : http://guides.rubyonrails.org/migrations.html

 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: