The next step in creating Rails Applications is to generate Migrations that simplify the creating and maintaining of database tables and columns. Rails Migration allows you to use Ruby to define changes to your database schema, making it possible to use a version control system to keep things synchronized with the actual code.
The following are some of its many uses−
It is to be noted that the activities done by Rails Migration can be done using any front-end GUI or directly on SQL prompt, but Rails Migration makes all those activities very easy.
The generic syntax for creating a migration is −
This syntax will create the file db/migrate/001_table_name.rb. A migration file contains the basic Ruby syntax that describes the data structure of a database table.
Make sure to clean the existing migrations generated by model generators before running the migration generator.
Now, let us create two migrations corresponding to our three tables − books and subjects.
Books migration should be as follows −
This generates the following code.
subject migration should be as follows −
It is important to note that you are using lower case for book and subject and plural form while creating migrations. This is a Rails paradigm that you should follow each time you create a Migration.
Next, go to db/migrate subdirectory of your application and edit each file one by one using any simple text editor.
Here is how to modify 001_books.rb. The ID column will be created automatically, so don't do it here as well.
The self.up method is used when migrating to a new version and self.down for vice versa. That is, to roll back any changes if needed. At this moment, the above script will be used to create books table.
Modify 002_subjects.rb as follows −
class Subjects < ActiveRecord::Migration def self.up create_table :subjects do |t| t.column :name, :string end Subject.create :name => "Physics" Subject.create :name => "Mathematics" Subject.create :name => "Chemistry" Subject.create :name => "Psychology" Subject.create :name => "Geography" end def self.down drop_table :subjects end end<>The above script will be used to create subjects table and will create five records in the subjects table.>
“Schema_info" tracks the current version of the database. The above code will create a "schema_info" table if it doesn't exist, which - each new migration will be a new version, and any new migrations will be run until your database is at the current version.
Rake is a Ruby build program similar to Unix make program that Rails takes advantage of, to simplify the execution of complex tasks such as updating a database's structure etc.
Use the RAILS_ENV shell variable, if you would like to specify what Rails environment to use for the migration,.
For example −
"set RAILS_ENV = production" is to be used in Windows instead of export command.
Ruby on Rails Related Interview Questions
|Python Interview Questions||HTML 5 Interview Questions|
|PHP Interview Questions||PostgreSQL Interview Questions|
|Ruby Interview Questions||Django Interview Questions|
|wxPython Interview Questions||MongoDB Interview Questions|
|Python Automation Testing Interview Questions||GitHub Interview Questions|
|Ruby Sinatra Interview Questions|
Ruby On Rails Tutorial
Ruby On Rails
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.