Disable ActiveRecord for Rails 4

2019-01-03 11:31发布

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)

8条回答
何必那么认真
2楼-- · 2019-01-03 12:11

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 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.

查看更多
Melony?
3楼-- · 2019-01-03 12:15

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.

查看更多
在下西门庆
4楼-- · 2019-01-03 12:15

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"
查看更多
Bombasti
5楼-- · 2019-01-03 12:20

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

查看更多
劳资没心,怎么记你
6楼-- · 2019-01-03 12:20

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
查看更多
叼着烟拽天下
7楼-- · 2019-01-03 12:22

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

查看更多
登录 后发表回答