118

I want to disable ActiveRecord in Rails 4. I did the following in config/application.rb

require File.expand_path('../boot', __FILE__)

# require 'rails/all'  -- commented

require "action_controller/railtie"
require "action_mailer/railtie"
#require "active_resource/railtie" no need
#require "rails/test_unit/railtie" no need
#require "sprockets/railtie" no need

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env)

module MyApp
  class Application < Rails::Application
     config.app_middleware.delete "ActiveRecord::ConnectionAdapters::ConnectionManagement"
  end
end

By I have an error of

/home/alex/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0/lib/rails/railtie/configuration.rb:95:in 
  method_missing: undefined method active_record for #<Rails::Application::Configuration:0x00000002005c38> (NoMethodError)
Deepak Mahakale
  • 22,834
  • 10
  • 68
  • 88
Incerteza
  • 32,326
  • 47
  • 154
  • 261

8 Answers8

275

If you are creating a new application, you can use -O to skip ActiveRecord:

rails new my_app -O

For existing applications:

1. Remove database adapter gems from your Gemfile (mysql2, sqlite3, etc.)

2. Change your config/application.rb

Remove require 'rails/all line and require frameworks (among those available in your rails version, the list varies, do not just copy) you want to use, for example:

require "action_controller/railtie"
require "action_mailer/railtie"
require "sprockets/railtie"
require "rails/test_unit/railtie"

Remove config.active_record.raise_in_transactional_callbacks = true from config/application.rb

3. Delete your config/database.yml file, db/schema.rb and migrations (if any)

4. Delete migration check in test/test_helper.rb

5. Delete any ActiveRecord configuration from your config/environments files (this is what is causing your error)

This is all you need to do for an empty Rails app. If you run into problems caused by your existing code, stack trace should give you sufficient information on what you need to change. You might for example have some ActiveRecord configuration in your initializers.

x-yuri
  • 16,722
  • 15
  • 114
  • 161
mechanicalfish
  • 12,696
  • 3
  • 46
  • 41
  • 2
    In addition to these changes, I also had to remove a couple of lines from spec_helpers.rb (using RSpec): about fixtures and transactions. I was also using active_model, so I replaced active_record with active_model in the requires list from here: http://stackoverflow.com/questions/19078044/disable-activerecord-for-rails-4 – or9ob Oct 21 '14 at 22:30
  • 3
    Where can I see a list of the available Rails frameworks, to be required in lieu of 'rails/all'? – emilesilvis Feb 19 '15 at 06:17
  • 19
    The list of everything `require 'rails/all'` includes can be found [here](https://github.com/rails/rails/blob/master/railties/lib/rails/all.rb). – nates Mar 03 '15 at 22:15
  • 3
    I created an application with `rails new my_app -O` a month ago. Now I want the active record back. What are files/gems/configuration I need to add now? – Jak Mar 25 '15 at 20:09
  • This is really handy, thanks. Although I also found it helpful to include `require 'active_model'` in application.rb (I'm using the validations from ActiveModel, but not the db interactions from ActiveRecord). – AJFaraday Jun 03 '15 at 19:01
  • 1
    I also had to remove `config.active_record.raise_in_transactional_callbacks = true` from `config/application.rb`. – B Seven Aug 24 '17 at 21:43
  • Works in Rails 5.1.4 as well. – B Seven Feb 03 '18 at 05:38
  • 1
    Doing this in Rails 6 I found I needed to do as in this post https://stackoverflow.com/a/60041361/8940624 – Chris A Feb 03 '20 at 17:18
  • What are "migration check in test/test_helper.rb", might I ask? – x-yuri Apr 30 '20 at 22:30
16

Hi this is what the default rails new new_app -O gives

require "rails"
# Pick the frameworks you want: 
require "active_model/railtie" 
require "active_job/railtie"
# require "active_record/railtie" 
require "action_controller/railtie" 
require "action_mailer/railtie" 
require "action_view/railtie" 
require "sprockets/railtie" 
require "rails/test_unit/railtie"

inside your config/application.rb

Additionally, it comes without database.yml and NO db/migrate/* and schema.rb

jasmo2
  • 525
  • 7
  • 13
8

Since this is still the first hit when searching Google for disabling active record for Rails 5, I'll add this here:

For Rails 5

Do all the steps in @mechanicalfish answer, but also remove the line

Rails.application.config.active_record.belongs_to_required_by_default = true

from

config/initializers/new_framework_defaults.rb
Deepak Mahakale
  • 22,834
  • 10
  • 68
  • 88
mmeyers
  • 89
  • 1
  • 2
5

For those using the rails-api gem you may encounter a similar error when using the --skip-active-record flag when doing rails-api new my_api. The current fix (until a new corrected version of the gem is released) is to edit your rails-api gem to have this commit. Use bundle open and replace the old Gemfile with the new corrected one. Rerun and you should be all set.

Alex Moore-Niemi
  • 2,913
  • 2
  • 24
  • 22
4

For disable ActiveRecord in Rails 4.2 you may create config/initializers/middleware.rb

Rails.application.middleware.tap do |middleware|
  middleware.delete ActiveRecord::Migration::CheckPending
  middleware.delete ActiveRecord::ConnectionAdapters::ConnectionManagement
  middleware.delete ActiveRecord::QueryCache
end

See the terminal rake middleware

Vinicius Luiz
  • 49
  • 1
  • 3
4

For Rails 5:

If you are generating a new app

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

rails new myApp --skip-active-record

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

ckhatton
  • 1,359
  • 1
  • 14
  • 43
  • 1
    In case you have already created your project without skipping active record, Just comment all active_record references in config/environments/ # config.active_record.verbose_query_logs = true # config.active_record.migration_error = :page_load And rename your database.yml file to something else. That did it for me. – Juan Ricardo Mar 11 '19 at 15:25
  • 1
    also comment this line in bin/setup => # system! 'bin/rails db:setup' – Juan Ricardo Mar 11 '19 at 16:02
1

For Rails Plugins (or gems) with a spec/dummy app

When your rails app lives in spec/dummy and you start your server from the plugin-root directory. You might still getting following error:

Cannot load `Rails.application.database_configuration`: Could not load database configuration. No such file - ["config/database.yml"] 

To avoid this, remove require rails/all inside the file bin/rails and require frameworks you want to use, for example:

# Pick the frameworks you want: 
require "active_model/railtie" 
require "active_job/railtie"
# require "active_record/railtie" 
require "action_cable/engine"
require "action_controller/railtie" 
require "action_mailer/railtie" 
require "action_view/railtie" 
require "sprockets/railtie" 
require "rails/test_unit/railtie"
rya brody
  • 206
  • 2
  • 4
-1

For Ruby On Rails version 5.1.x

require "rails"
# Pick the frameworks you want:
require "active_model/railtie"
require "active_job/railtie"
# require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_view/railtie"
require "action_cable/engine"
require "sprockets/railtie"
require "rails/test_unit/railtie"