Ruby-on-rails 在 Rails 3 中删除 ActiveRecord
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2212709/
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
Remove ActiveRecord in Rails 3
提问by Mark Embling
Now that Rails 3 beta is out, I thought I'd have a look at rewriting an app I have just started work on in Rails 3 beta, both to get a feel for it and get a bit of a head-start. The app uses MongoDB and MongoMapper for all of its models and therefore has no need for ActiveRecord. In the previous version, I am unloading activerecord in the following way:
现在 Rails 3 测试版已经出来了,我想我应该看看重写一个我刚刚开始在 Rails 3 测试版中工作的应用程序,既要感受一下,又要抢先一步。该应用程序的所有模型都使用 MongoDB 和 MongoMapper,因此不需要 ActiveRecord。在之前的版本中,我是通过以下方式卸载activerecord的:
config.frameworks -= [ :active_record ] # inside environment.rb
In the latest version this does not work - it just throws an error:
在最新版本中,这不起作用 - 它只会引发错误:
/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*
Of course, I have looked at the boot.rb as it suggested, but as far as I can see, there is no clue here as to how I might go about unloading AR. The reason I need to do this is because not only is it silly to be loading something I don't want, but it is complaining about its inability to make a DB connection even when I try to run a generator for a controller. This is because I've wiped database.ymland replaced it with connection details for MongoDB in order to use this gistfor using database.yml for MongoDB connection details. Not sure why it needs to be able to initiate a DB connection at all just to generate a controller anyway....
当然,我已经按照它的建议查看了 boot.rb,但据我所知,这里没有关于如何卸载 AR 的线索。我需要这样做的原因是,不仅加载我不想要的东西很愚蠢,而且它抱怨即使当我尝试为控制器运行生成器时也无法建立数据库连接。这是因为我已将其擦除database.yml并替换为 MongoDB 的连接详细信息,以便使用此要点将 database.yml 用于 MongoDB 连接详细信息。不知道为什么它需要能够启动一个数据库连接只是为了生成一个控制器......
Is anyone aware of the correct Rails 3 way of doing this?
有没有人知道正确的 Rails 3 方法?
回答by Stéphan Kochen
I'm going by this from reading the source, so let me know if it actually worked. :)
我正在阅读源代码,所以让我知道它是否真的有效。:)
The railscommand that generates the application template now has an option -O, which tells it to skip ActiveRecord.
rails生成应用程序模板的命令现在有一个选项-O,它告诉它跳过 ActiveRecord。
If you don't feel like rerunning rails, you should check the following in your existing app:
如果您不想重新运行rails,则应在现有应用程序中检查以下内容:
Check that your
config/application.rbdoesn'thaverequire 'rails/all'orrequire "active_record/railtie". Instead, for a standard Rails setup without ActiveRecord, it should have onlythe 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.envIf, in
config/application.rb, you are using theconfig.generatorssection, make sure it doesn't have the lineg.orm :active_record. You can set this explicitly tonil, if you want, but this should be the default wheng.ormis completely omitted.Optional, but in your
Gemfile, remove thegemline that loads the module for your database. This could be the linegem "mysql"for example.
检查您
config/application.rb不具有require 'rails/all'或require "active_record/railtie"。相反,对于没有 ActiveRecord 的标准 Rails 设置,它应该只有以下要求: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如果在 中
config/application.rb,您正在使用该config.generators部分,请确保它没有该行g.orm :active_record。nil如果需要,您可以将其显式设置为,但这应该g.orm是完全省略时的默认值。可选,但在您的 中
Gemfile,删除gem为您的数据库加载模块的行。gem "mysql"例如,这可能是这条线。
回答by apeiros
Rails 4
导轨 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).
我一直在寻找如何在 rails 4 中禁用它,但只找到了在 rails 4 中不再有效的这个答案。所以这就是你可以在 rails 4 中执行它的方法(在 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'orgem 'pg'. In config/application.rb, replace
require 'rails/all'withrequire "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_loadPotentially 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)
- 在您的 Gemfile 中,删除数据库驱动程序 gem,例如
gem 'sqlite3'或gem 'pg'。 在 config/application.rb 中,替换
require 'rails/all'为require "action_controller/railtie" require "action_mailer/railtie" require "sprockets/railtie" require "rails/test_unit/railtie"
在 config/environments/development.rb 中,删除或注释掉
config.active_record.migration_error = :page_load可能您必须从 spec_helper 中删除 active_record 助手(通过评论中的 VenoM)
可能您必须删除 ConnectionManagement 中间件(独角兽似乎就是这种情况):(
config.app_middleware.delete "ActiveRecord::ConnectionAdapters::ConnectionManagement"通过https://stackoverflow.com/a/18087332/764342)
I hope this helps others looking for how to disable ActiveRecord in Rails 4.
我希望这可以帮助其他人寻找如何在 Rails 4 中禁用 ActiveRecord。
回答by Jim Geurts
For a new rails app, you can have it exclude active record by specifying the --skip-active-record parameter. Eg:
对于新的 rails 应用程序,您可以通过指定 --skip-active-record 参数让它排除活动记录。例如:
rails new appname --skip-active-record
回答by vlad
If you generated a new project using Rails 3.2, you will also need to comment out:
如果您使用 Rails 3.2 生成了一个新项目,您还需要注释掉:
config.active_record.mass_assignment_sanitizer = :strict
and
和
config.active_record.auto_explain_threshold_in_seconds = 0.5
in your development.rbfile.
在您的development.rb文件中。
回答by Michal Kuklis
All of the above are true. The one more thing which I had to do in rails 3.1 is to comment out
以上都是真的。我必须在 Rails 3.1 中做的另一件事是注释掉
config.active_record.identity_map = true
in config/application.rb.
在config/application.rb。
回答by Rimian
If you're running rspec, you also need to remove (in spec_helper):
如果您正在运行 rspec,则还需要删除(在 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
回答by Andrew Lank
Also commentout
还评论了
# config/application.rb
config.active_record.whitelist_attributes = true
(noted on rails 3.2.13)
(在 Rails 3.2.13 上注明)

