Rails and Rake Commands and Rails Environments


Rails seems to consist of two parts, a set of components to build an application on top of and a command line tool to help this process. The reason the rails command is so useful is the amount of convention in a Rails app: Certain files with certain names are expected in certain locations. When one file or class is created, the chances are that a number of further files and classes will be required. The most commonly used rails commands are:

rails new

The first command is rails new app_name. This can take a number of options to specify versions, templates, databases, or to skip certain parts of the application. View a full list with rails new –h. Rails new appname –t is possibly useful to skip creating the test folder if you intend to use rspec.

rails server

Runs the default rails dev server, WEBrick. which can be viewed on localhost:3000. Again use –h to view all the options. The –d option runs the server as a daemon and –u enables debugging. One option to be aware of is –e which allows you to specify an environment to run under, more on this later. rails s can be substituted for rails server.

rails generate

Creates boilerplate code for various parts of an application. Installing a gem (such as rspec) can add additional generators. The most common rails generators are probably scaffold, model, controller and migration. rails g can be used in place of rails generate.

  • rails g controller controller_name action1 action2 … generates a controller with the specified actions and views to match the actions. e.g. rails generate controller Products buy sell will create app/controllers/products_controller.rb which will contain class ProductsController with methods buy and sell. It will also create the views app/views/products/buy.html.erb and app/views/products/sell.html.erb. The command will also create routes, test classes and helpers.
  • rails g model model_name field1:type field2:type … creates a class for the model in app/models/model_name.rb and a database migration file to add a matching table to the database. It also creates a test class and a fixture file (to provide test data).
  • rails g migration migration_name  creates a migration file with the specified name (preceded by a timestamp). To add colums to an existing table rails g migration add_something_to_tablename field1:type field2:type can be used to create the correct migration automatically.
  • rails g scaffold model_name field1:type field2:type … Combines all of the above, creating a model and migration, a controller with basic CRUD actions, matching views, tests, helpers etc. Although this would appear useful it is meant to be more of a learning tool (and useful for the various instant gratification demo’s of Rails).

rails console

Starts a console that can run ruby code. Like the rails server an environment can be specified to run under e.g. rails console test (note this is different to rails server).


Rake is a ruby command for ‘ruby make’ used for common administration tasks. The most common tasks are to administer databases and run tests.

rake test

Runs the applications tests, a number of options can be used to restrict the tests that are run.

rake db:command

There are a number of db commands but the most used is db:migrate. This command can take further options, one of which is used to specify the rails environment – rake db:migrate RAILS_ENV=test (and a third syntax to specify the environment.


After running rails new appname, three environments will be available, development, production, and test. The settings for the environments are specified in config/environments /*.rb and as seen above, various commands can specify the environment they run under. Further environments can be created if required e.g. staging.

The default environment for rails server, console and rake db:migrate is development. Tests run in the test environment. Running under other environments is normally done by specifying the environment in the server or hosting configuration.

The rails environment is useful in a number of other places such as:

Testing the environment from code - 

<%= debug(params) if Rails.env.development? %>

Loading certain gems for specific environments with groups in the gemfile

source 'http://rubygems.org'

gem 'rails', '3.0.3'
gem 'sqlite3-ruby', '1.2.5', :require => 'sqlite3'
gem 'gravatar_image_tag', '0.1.0'

group :development do
  gem 'rspec-rails', '2.1.0'
  gem 'annotate-models', '1.0.4'

group :test do
  gem 'rspec', '2.1.0'
  gem 'webrat', '0.7.1'
  gem 'autotest', '4.3.2'
  gem 'autotest-fsevent', '0.2.2'
  gem 'autotest-growl', '0.2.8'
  gem 'autotest-rails-pure', '4.1.0'
  gem 'spork', '0.8.4'
  gem 'factory_girl_rails', '1.0'

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.