删除 Rails 3中的 ActiveRecord

既然 Rails 3测试版已经发布了,我想我应该重写一下我刚开始编写的 Rails 3测试版的应用程序,这样既可以感受一下,又可以提前一些。该应用程序的所有模型都使用 MongoDB 和 MongoMapper,因此不需要 ActiveRecord。在前一个版本中,我通过以下方式卸载 activerrecord:

config.frameworks -= [ :active_record ]    # inside environment.rb

在最新版本中,这种方法不起作用——它只是抛出一个错误:

/Library/Ruby/Gems/1.8/gems/railties-3.0.0.beta/lib/rails/configuration.rb:126:in
`frameworks': config.frameworks in no longer supported. See the generated
config/boot.rb for steps on how to limit the frameworks that will be loaded
(RuntimeError)
from *snip*

当然,我已经按照建议查看了 boot.rb,但就我所知,这里没有关于如何卸载 AR 的线索。我需要这样做的原因不仅是因为装载我不想要的东西是愚蠢的,而且它抱怨它无法建立一个 DB 连接,即使我尝试为控制器运行一个生成器。这是因为我清除了 database.yml,并将其替换为 MongoDB 的连接详细信息,以便使用 这个要点来使用 datase.yml 来处理 MongoDB 的连接详细信息。不知道为什么它需要能够启动一个 DB 连接,只是为了生成一个控制器..。

有人知道正确的 Rails 3做这件事的方法吗?

36099 次浏览

I'm going by this from reading the source, so let me know if it actually worked. :)

The rails command that generates the application template now has an option -O, which tells it to skip ActiveRecord.

If you don't feel like rerunning rails, you should check the following in your existing app:

  • Check that your config/application.rb doesn't have require 'rails/all' or require "active_record/railtie". Instead, for a standard Rails setup without ActiveRecord, it should have only the following requires:

    require File.expand_path('../boot', __FILE__)
    
    
    require "action_controller/railtie"
    require "action_mailer/railtie"
    require "active_resource/railtie"
    require "rails/test_unit/railtie"
    require "sprockets/railtie"
    
    
    # Auto-require default libraries and those for the current Rails environment.
    Bundler.require :default, Rails.env
    
  • If, in config/application.rb, you are using the config.generators section, make sure it doesn't have the line g.orm :active_record. You can set this explicitly to nil, if you want, but this should be the default when g.orm is completely omitted.

  • Optional, but in your Gemfile, remove the gem line that loads the module for your database. This could be the line gem "mysql" for example.

All of the above are true. The one more thing which I had to do in rails 3.1 is to comment out

config.active_record.identity_map = true

in config/application.rb.

For a new rails app, you can have it exclude active record by specifying the --skip-active-record parameter. Eg:

rails new appname --skip-active-record

If you generated a new project using Rails 3.2, you will also need to comment out:

config.active_record.mass_assignment_sanitizer = :strict

and

config.active_record.auto_explain_threshold_in_seconds = 0.5

in your development.rb file.

Also comment out

# config/application.rb
config.active_record.whitelist_attributes = true

(noted on rails 3.2.13)

If you're running rspec, you also need to remove (in spec_helper):

  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"

and remove

  # If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true

Rails 4

I was looking for how to disable it in rails 4 and only found this answer which no longer works in rails 4. So this is how you can do it in rails 4 (tested in RC1).

In a new project

rails new YourProject --skip-active-record

In an existing project

  • In your Gemfile, remove the database driver gem, e.g. gem 'sqlite3' or gem 'pg'.
  • In config/application.rb, replace require 'rails/all' with

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

  • In config/environments/development.rb, remove or comment out config.active_record.migration_error = :page_load

  • Potentially you have to remove active_record helpers from the spec_helper (via VenoM in the comments)

  • Potentially you have to remove the ConnectionManagement middleware (seems to be the case with unicorn): config.app_middleware.delete "ActiveRecord::ConnectionAdapters::ConnectionManagement" (via https://stackoverflow.com/a/18087332/764342)

I hope this helps others looking for how to disable ActiveRecord in Rails 4.