Ruby-on-rails 禁用 Rails 4 的 ActiveRecord

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19078044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:46:08  来源:igfitidea点击:

Disable ActiveRecord for Rails 4

ruby-on-railsrubyruby-on-rails-4

提问by アレックス

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

我想在 Rails 4 中禁用 ActiveRecord。我在 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)

回答by mechanicalfish

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

如果你正在创建一个新的应用程序,你可以使用-O跳过 ActiveRecord:

rails new my_app -O

For existing applications:

对于现有应用:

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

1. 从您的 Gemfile(mysql2、sqlite3 等)中删除数据库适配器 gems

2. Change your config/application.rb

2. 改变你的 config/application.rb

Remove require 'rails/allline and require frameworks (among those availablein your railsversion, the list varies, do not just copy) you want to use, for example:

删除要使用的require 'rails/all行和需要框架(在您的版本中可用的框架中rails,列表各不相同,不要只是复制),例如:

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

Remove config.active_record.raise_in_transactional_callbacks = truefrom config/application.rb

删除config.active_record.raise_in_transactional_callbacks = trueconfig/application.rb

3. Delete your config/database.ymlfile, db/schema.rband migrations (if any)

3. 删除您的config/database.yml文件db/schema.rb和迁移(如果有)

4. Delete migration check in test/test_helper.rb

4. 删除迁移签入 test/test_helper.rb

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

5. 从您的config/environments文件中删除任何 ActiveRecord 配置(这是导致您出错的原因)

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.

这就是你需要为一个空的 Rails 应用程序做的所有事情。如果您遇到由现有代码引起的问题,堆栈跟踪应该为您提供有关需要更改的内容的足够信息。例如,您的初始值设定项中可能有一些 ActiveRecord 配置。

回答by jasmo2

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

嗨,这是默认的 rails new new_app -O 给出的

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

在你的config/application.rb里面

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

此外,它没有database.yml并且没有db/migrate/*schema.rb

回答by mmeyers

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

由于这仍然是搜索 Google 以禁用 Rails 5 的活动记录时的第一次命中,因此我将在此处添加:

For Rails 5

对于 Rails 5

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

执行@mechanicalfish 回答中的所有步骤,但也要删除该行

Rails.application.config.active_record.belongs_to_required_by_default = true

from

config/initializers/new_framework_defaults.rb

回答by Alex Moore-Niemi

For those using the rails-api gemyou may encounter a similar error when using the --skip-active-recordflag 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 openand replace the old Gemfilewith the new corrected one. Rerun and you should be all set.

对于那些使用rails-api gem的人,--skip-active-record在执行rails-api new my_api. 当前的修复(直到 gem 的新更正版本发布)是编辑您的 rails-api gem 以进行此提交。使用bundle open并用Gemfile新的更正的替换旧的。重新运行,你应该一切都准备好了。

回答by Vinicius Luiz

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

要在 Rails 4.2 中禁用 ActiveRecord,您可以创建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

查看终端rake 中间件

回答by Crimbo

For Rails 5:

对于 Rails 5:

If you are generating a new app

如果您要生成新应用

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

使用--skip-active-record选项生成没有数据库的应用程序:

rails new myApp --skip-active-record

rails new myApp --skip-active-record

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

请注意与以前版本的 Rails 不同的额外连字符“-”。

回答by rya brody

For Rails Plugins (or gems) with a spec/dummyapp

对于带有spec/dummy应用程序的Rails 插件(或 gems)

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

当你的 rails 应用程序存在spec/dummy并且你从 plugin-root 目录启动你的服务器时。您可能仍会收到以下错误:

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

To avoid this, remove require rails/allinside the file bin/railsand require frameworks you want to use, for example:

为避免这种情况,请删除require rails/all文件内部bin/rails并需要您要使用的框架,例如:

# 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"

回答by Kamil Za??ski

For Ruby On Rails version 5.1.x

对于 Ruby On Rails 版本 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"