Ruby on Rails
Console

The console gives you access to your Rails Environment where you can interact with the domain model . Here you’ll have all parts of the application configured, just like it is when the application is running. You can inspect domain models, change values, and save to the database.

Start the console with the console script:

script/console

Starting the script without arguments will start the console in the development environment. Use script/console --help to see the available options.

NAME
     console - interact with the domain model through a environment console (on IRB)

SYNOPSIS
     console [environment]

DESCRIPTION
     Starts an environment console using IRB that lets you manipulate and interrogate
     the domain model or even trigger controller actions. The database connection and
     configuration available to the web application is already setup.

     Tab completion is available to see classes and methods on individual objects.

EXAMPLE
     console production

     This will initialize the production environment (as setup in config/database.yml
     and config/environments/production.rb). You would now be ready to start requiring
     models using require_dependency.

Use quit or exit when finished with console.

Problems Running Console

If you get this error when trying to start the console:

/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:18:in
`require__': No such file to load -- readline (MissingSourceFile)
...

make sure you have the ReadlineLibrary properly installed.

If you do not wish to use the readline library you can simply comment out the following line in script/console:

libs << " -r irb/completion"

Reloading Classes

With the later version of Rails, you can reload your models and controllers with the following command. Be sure to recreate any instances of your models as they will still be using the old class


reload!

To reset your application you may want to try


Dispatcher.reset_application!
(Does this work with rails 2.0?) JHS

Also see http://habtm.com/articles/2005/10/04/script-console-reload-models

See also IrbAndConsoleAndWindows.

Tracing with script/console

Did you read an ‘irb Mix tape’ article and wondering how you can turn tracer on using script/console? Well, run


script/console —irb=‘irb —tracer’

and that’s it. Now you can get a full trace of anything and if you really have lots of patience, you can debug app.get '/' :-)