113

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

Rimian
  • 36,864
  • 16
  • 117
  • 117
RobbieCanuck
  • 1,551
  • 3
  • 11
  • 11

6 Answers6

118

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

Community
  • 1
  • 1
RSK
  • 17,210
  • 13
  • 54
  • 74
  • 2
    yes, this works with Rails 4 :) - just validated. If you want to ditch test unit use: rails new myApp-O --skip-bundle -T – Chris Hough Oct 08 '13 at 07: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 – Nic Benders Nov 13 '13 at 06:17
  • 1
    I just faced the issues and noticed. For Rails 4.2.x only `-O` works. The option `--skip-activerecord` does not work. – Nazar Hussain Jul 11 '15 at 23:12
  • This works in rails-api too: rails new myApp -O . Thanks – Abel Aug 10 '15 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 Oct 20 '16 at 02:24
  • For rails 7 `rails new myApp -O` – antonkronaj Feb 15 '23 at 16:01
67

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.

Alter Lagos
  • 12,090
  • 1
  • 70
  • 92
  • 2
    For Rails 5 you might also want to require `active_job/railtie` and `action_cable/engine` – Ingmaras Keleras Jul 06 '16 at 14:35
  • 2
    Isn't there a dummy DB adapter to use in `database.yml` so it will just ignore the DB? – Chloe Mar 02 '17 at 08:18
  • Rails 5 leads then to the problem of failing `rake assets:precompile` due to `NoMethodError: undefined method `active_record'` :/ – flp Apr 19 '17 at 14:46
  • I guess also the following in `config/initializers/new_framework_defaults.rb` should be commented: `Rails.application.config.active_record.belongs_to_required_by_default = true ` – flp Apr 19 '17 at 15:11
  • 4
    @Chloe The `activerecord-nulldb-adapter` from https://github.com/nulldb/nulldb allows you to swap out the adapter for a temporary or less invasive change. – Unixmonkey Apr 01 '19 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 Dec 12 '19 at 21:43
  • Think I figured it out, see https://stackoverflow.com/a/59325729/721065 – cdmo Dec 13 '19 at 15: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. – Alter Lagos Dec 13 '19 at 21:04
  • @AlterLagos yes, you are right, ActionMailer can be enabled. I had forgotten that I removed it for a different reason in the app I am working on. Thanks for updating your canonical answer, I will remove mine – cdmo Dec 16 '19 at 14:27
55

Uncomment this line in the environment.rb file:

config.frameworks -= [ :active_record, :active_resource, :action_mailer]
CharlesL
  • 265
  • 5
  • 21
typemismatch
  • 2,047
  • 3
  • 23
  • 26
17

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

sunsations
  • 369
  • 2
  • 10
John Barela
  • 196
  • 2
  • 4
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.

vrish88
  • 20,047
  • 8
  • 38
  • 56
  • 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. – RobbieCanuck May 04 '09 at 18:42
  • 1
    I disagree. Active.com is built on Rails with no database because all the data comes from an api. – jspooner Aug 06 '13 at 15:05
  • Note: The second link in the last sentence no longer exists. I get a 404 not found error. – Pamela Cook - LightBe Corp Feb 14 '17 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
Sergii Chub
  • 107
  • 1
  • 3