118

It would be convenient to use Ruby on Rails for a small website project that has no current need for a database. I know I could create an empty database in MySQL and go from there, but does anyone know a better way to run Rails without a database?

Thanks

6 Answers 6

122

For Rails 3 and Rails 4:

Use -O(Capital 'O') or --skip-activerecord option to generate an application without a database.

rails new myApp -O

or

rails new myApp --skip-activerecord

This Answer is reshared from here


For Rails 5:

Use --skip-active-record option to generate an application without a database

Notice the extra hyphen '-' as opposed to previous Rails versions.

rails new myApp --skip-active-record

6
  • 2
    yes, this works with Rails 4 :) - just validated. If you want to ditch test unit use: rails new myApp-O --skip-bundle -T Commented Oct 8, 2013 at 7:01
  • 1
    This is definitely the easiest way to go if you are starting from scratch, although be warned that (at least in Rails 4.0.1) there is a '-' between active and record in that command. So it should read: rails new myApp --skip-active-record Commented Nov 13, 2013 at 6:17
  • 1
    I just faced the issues and noticed. For Rails 4.2.x only -O works. The option --skip-activerecord does not work. Commented Jul 11, 2015 at 23:12
  • This works in rails-api too: rails new myApp -O . Thanks Commented Aug 10, 2015 at 23:37
  • 2
    In case anyone is wondering, in Rails 5 it's --skip-active-record (note the dash between active nad record). The other way doesn't work for me.
    – audiodude
    Commented Oct 20, 2016 at 2:24
69

For new projects, with a simple rails new your_app --skip-active-record will work, but for an existing Rails 4-7 project, in your config/application.rb file you have the following line:

require 'rails/all' # or `require "rails"' in newer versions

(As reference that line is loading this file)
So instead of load ALL, you must to load each library separately as follows:

# active_record is what we're not going to use it, so add it comment in case 
# at some point you enable it back again
# require "active_record/railtie" 

# This is not loaded in rails/all but inside active_record so add it if
# you want your models work as expected
require "active_model/railtie" 
# And now the rest
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_view/railtie"
require "active_job/railtie" # Only for Rails >= 4.2
require "action_cable/engine" # Only for Rails >= 5.0
require "sprockets/railtie" # Deprecated for Rails >= 7, so add it only if you're using it
require "rails/test_unit/railtie"

# All these depend on active_record, so they should be excluded also
# require "action_text/engine" # Only for Rails >= 6.0
# require "action_mailbox/engine" # Only for Rails >= 6.0
# require "active_storage/engine" # Only for Rails >= 5.2

Keep an eye to the comments to know what to load regarding your Rails version.
Also check the following files (in case you have them) and comment the following lines:

# package.json
"@rails/activestorage": "^6.0.0",

# app/javascript/packs/application.js
require("@rails/activestorage").start()

# bin/setup
system! 'bin/rails db:prepare'

# config/environments/development.rb
config.active_storage.service = :local # For Rails >= 5.2
config.active_record.migration_error = :page_load
config.active_record.verbose_query_logs = true

# config/environments/test.rb
config.active_storage.service = :test # For Rails >= 5.2

# config/environments/production.rb
config.active_storage.service = :local # For Rails >= 5.2
config.active_record.dump_schema_after_migration = false

# spec/rails_helper.rb
ActiveRecord::Migration.maintain_test_schema!

# test/test_helper.rb
fixtures :all # In case you're using fixtures

# Only for Rails >= 5.0
#config/initializers/new_framework_defaults.rb
Rails.application.config.active_record.belongs_to_required_by_default = true

Also remove any reference to ActiveRecord::Base in your model files (or simply delete the files if apply). For example, the autogenerated app/models/application_record.rb file.

9
  • 2
    For Rails 5 you might also want to require active_job/railtie and action_cable/engine Commented Jul 6, 2016 at 14:35
  • 2
    Isn't there a dummy DB adapter to use in database.yml so it will just ignore the DB?
    – Chloe
    Commented Mar 2, 2017 at 8:18
  • 4
    @Chloe The activerecord-nulldb-adapter from github.com/nulldb/nulldb allows you to swap out the adapter for a temporary or less invasive change.
    – Unixmonkey
    Commented Apr 1, 2019 at 19:55
  • 1
    This nearly worked for me in a Rails 6 project, however, the app errors out on local development initial page load and then runs fine on subsequent loads. Puma caught this error: Cannot load database configuration: Could not load database configuration. No such file - ["config/database.yml"] (RuntimeError) /Users/cdm32/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/railties-6.0.1/lib/rails/application/configuration.rb:240:in `database_configuration\' /Users/cdm32/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activerecord-6.0.1/lib/active_record/railtie.rb:200:in block (2 level...`
    – cdmo
    Commented Dec 12, 2019 at 21:43
  • 1
    @cdmo hey, thanks for noticing. I've updated the answer to make it fully compatible with rails 6. Not sure about all you commented in your answer as I create 2 rails apps with and without -O and action_mailer isn't related with config/detabase.yml, so there's no need to comment it. Also there are some extra lines to comment after active_storage is commented. Commented Dec 13, 2019 at 21:04
55

Uncomment this line in the environment.rb file:

config.frameworks -= [ :active_record, :active_resource, :action_mailer]
4
18

In Rails 4 when starting a new project you can use -O or --skip-active-record

rails new my_project -O
rails new my_project --skip-active-record

If you've already created a project you will need to comment

 require "active_record/railtie"

from config/application.rb and

 config.active_record.migration_error = :page_load

from config/environments/development.rb

1
  • Works in Rails 7.1
    – fguillen
    Commented May 24 at 18:49
14

If you don't need a database then you probably don't need to have the bulk of Rails. You may want a smaller more customizable framework to work with.

Sinatra is a tiny framework that is great for serving up basic static pages.

But if you insist on using Rails here is an article that will show you how to do just that or here.

3
  • Thanks - both of these links refer to the Rails Recipes book and contain steps to get testing working with a database-less rails application. I was being lazy and hoped to leverage the rails infrastructure already on the server for other apps, but Sintatra looks intriguing. Commented May 4, 2009 at 18:42
  • 1
    I disagree. Active.com is built on Rails with no database because all the data comes from an api.
    – jspooner
    Commented Aug 6, 2013 at 15:05
  • Note: The second link in the last sentence no longer exists. I get a 404 not found error. Commented Feb 14, 2017 at 20:11
1

For support Rails 6 rc1 and activerecord-nulldb-adaptergem we need a monkey patching

In config/initializers/null_db_adapter_monkey_patches.rb

module ActiveRecord
  module ConnectionAdapters
    class NullDBAdapter < ActiveRecord::ConnectionAdapters::AbstractAdapter
      def new_table_definition(table_name = nil, is_temporary = nil)
        TableDefinition.new(table_name, is_temporary)
      end
    end
  end
end

Not the answer you're looking for? Browse other questions tagged or ask your own question.