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:32

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.

查看更多
等我变得足够好
3楼-- · 2019-01-03 12:32

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"
查看更多
登录 后发表回答