Chapter 6. Into Production

Running a Rails application in the production environment is a crucial part of any project. It represents the successful completion of a working application and delivery of that application to its customer base. It is rarely the final stage. All applications we have worked on have needed modifying from their initial production incarnation.

So, what is needed to get a Rails application into the production environment?

  • A working Rails application.
  • A production server environment.
  • A system for transferring development Rails code to the production environment.

This chapter will describe how to create a Rails production environment. In particular, the decisions we need to make to successfully get a small business application up and running. Error handling in the production environment is a little different to that of the development environment and therefore some coverage of error handling is presented. We will also describe some systems that will make it easier to backup and restore your application.

An Application Ready for Production

In many Rails tutorials and guides there is a common theme: don't worry about the production environment, it is something you can sort out later. I strongly believe this is a mistake.

Note

Do not wait until your application is built before you create and test the production environment!

If you cannot get your application working in the production environment, any time you spent creating that application has been wasted.

Our application may be the most beautifully crafted piece of coding ever created, but if we cannot present it to our users, we might as well have created a chocolate teapot.

Therefore, it is important, before we spend time developing our application, that we create and test our production environment. This is particularly important with Rails because at the time of writing, creating a good production environment can be the most problematic part of Rails development. The good news is that solutions to most of the common problems are being developed and are coming on line all the time. In particular, the creation of Mongrel (a Ruby web server that will be discussed later in the chapter) has greatly simplified the process and removed the worst headaches.

Rory comes to similar conclusions. He does not want to spend all week creating his application just to find that it only works on his computer. The whole point of the task Ken has set for him is to make the contact data available to everyone in the company. Even if others can browse his computer, he does not want his system to slow down to provide everyone else with a new service. However, he also spots a conundrum: "I need an application to test the production environment, but I want to do the test before I create an application." The solution is to create a simple test application that can be passed to the production system and tested there. Scaffold makes it very easy to create a basic working application. So, Rory creates and develops a test application:

  1. Rory first creates a test application in an empty folder.
    $ rails TestApp
    
  2. Scaffold needs an existing SQL table to create the correct application code, to match the table structure. Moving into the application folder, Rory creates a test model first, so he can use migration to create the initial database table.
    $ ruby script/generate model TestThing
    
  3. He then modifies 001_create_test_things.rb by removing the comments. He thereby creates the migration code shown below. As this is a very basic test, only one database field is required.
    class CreateTestThings < ActiveRecord::Migration
    def self.up
    create_table :test_things do |t|
    t.column :name, :string
    end
    end
    def self.down
    drop_table :test_things
    end
    end
    
  4. Rory is using MySQL and decides to use the default Test schema for all the environments of this test application. He modifies database.yml to suit. The resulting database.yml is shown below. Notice that all environments use the same database schema and therefore have the same content.
    development:
    adapter: mysql
    database: Test
    username: root
    password: password
    host: localhost
    test:
    adapter: mysql
    database: Test
    username: root
    password: password
    host: localhost
    production:
    adapter: mysql
    database: Test
    username: root
    password: password
    host: localhost
    
  5. Rory then creates the new table using Rake.
    $ rake migrate
    
  6. Finally, he can create the scaffold, which he tests with the help of WEBrick or Mongrel. The application is available at:"http://localhost:3000/test_things/list"
$ ruby script/generate scaffold TestThing
$ ruby script/server

Rory, now, has a working Rails application that he can test in his production environment. It has only the minimum of functionality, but it is all that is needed to demonstrate the viability of the production environment.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.237.164