Making a Rails App Without a Database or ActiveRecord

Occasionally, you may want to build a Rails app without ActiveRecord or a database. Your application might only store data using third party APIs, or might not have any persistent data at all. Since Rails optimizes for the most common case (a database-backed app), it refuses to start the server without a database connection by default.

Luckily, it only takes three steps to make Rails work without a database:

  1. Remove any database gems (sqlite, pg, mysql, etc) from your Gemfile and then run bundle.
  2. Change your application.rb. The default app generated by Rails 4.0 requires all of Rails:
    require 'rails/all'

    This includes ActiveRecord, which requires a database connection. Instead, you must include the specific parts of Rails that you will be using, for example:

    require 'action_controller/railtie'
    require 'action_mailer/railtie'
    require 'active_model/railtie'
    require 'sprockets/railtie'
    require 'rails/test_unit/railtie'
  3. Remove any references to active_record in your configuration. The default app generated by Rails 4.0 has one reference to active record which must be removed, in config/environments/development.rb:
    config.active_record.migration_error = :page_load

With these changes, I was able to get the Rails application started up without a database connection. I also verified that these steps allow the app to be deployed to heroku without a database add-on.

Love making great software? Let’s talk!

Apply to work with us!

On the Revelry blog, our team shares tips, tricks, and strategy for building innovative products.
Check out our posts on Product, Team News, and Development.

More Posts by Robert Prehn: