Rapid Rails Part 1: Command Line Mastery

Rapid Rails is a series of articles containing succinct tips to increase your productivity when working with Ruby on Rails. This is the first part, and shows you how to make the most of the command–line tools that come with Rails.
Quickly generate migrations
The Rails generator script (found inside an application’s directory at script/generate) allows you to quickly create templates for anything you need within Rails. As well as models and controllers, you can also use it to write entire migrations:
script/generate migration AddIPAddressToUsers ip_address:string
The field definition is specified as field_name:type, and you can type in as many as you need for each migration. To read Rails' help on this feature, type:
script/generate migration
This cuts down the hassle of changing database schemas around, and should be the nail in the coffin for lazy database schema manipulation with GUI tools.
Get more generators
People have written generators to create templates for sophisticated applications, providing a great way of rapidly prototyping an application: Rails wiki generator list.
What was I doing last week?
The Rakefile that comes with Rails contains a handy way of searching for comments with phrases including TODO and FIXME:
rake notes rake notes:fixme rake notes:optimize rake notes:todo
An additional benefit of this is it can increase your understanding of recent code changes, especially when combined with version control logs.
More Rake house–keeping tools
To list all of the rake tasks, type rake --tasks. A few tasks I often use are:
rake tmp:cache:clear rake log:clear rake routes rake db:sessions:create # Creates a migration for ActiveRecord sessions
Get help (and how to cheat)
Getting ruby library help in the command–line is usually ri's job:
ri FileUtils#cp
The all–round maverick genius Mauricio Fernandez decided ri was too slow, and built FastRI. Not only is it faster than RI, but it can also work in a distributed fashion with Rinda. It’s easy to install and use:
sudo gem install fastri fastri-server & fri FileUtils#cp
If this isn’t enough help, why not use cheat sheets from the command–line?
sudo gem install cheat cheat rails_console
These Ruby–focussed cheat sheets are intuitive, with plenty of tips for those things you can’t be blamed for forgetting.
Testing and command–line flags
If you’re running a unit test and there’s a problem with one test, just run a single test with -n:
ruby test/unit/user_test.rb -n test_authenticate
There’s more flags too, just run a test with –h:
ruby test/unit/user_test.rb -h
Debugging
If you’re really stuck, you can breakpoint and trace to your heart’s content by installing ruby–debug:
sudo gem install ruby-debug
ruby–debug is a more pleasant with autoeval set, so create a text file called .rdebugrc in your home directory and add:
set autolist set autoeval set autoreload
Now, run rdebug with a ruby script:
rdebug script/server
Add a breakpoint (obviously set this to something meaningful within your application) and continue execution:
b app/controllers/application.rb:10
The script should now halt at your breakpoint. To read more, check out rdebug's home and a handy rdebug cheat sheet (or type cheat rdebug).
Reduce password entries during development
If your application uses a version control system on a remote server over SSH, consider creating SSH keys to cut down password entries. To do this securely, use ssh–agent. Read more here: SSH with Keys HOWTO
Speed up your development server: Mongrel and Thin
Mongrel and Thin are great alternatives to the built in Rails web server, and are both faster. To install use sudo gem install mongrel or sudo gem install thin (they’re both fast, thin appears to be slightly faster.) Then run one of the following commands from your application’s directory:
mongrel_rails start
Or:
thin start
Rails now automatically runs mongrel with script/server.