在 Rails 3 中删除 ActiveRecord [英] Remove ActiveRecord in Rails 3

查看:48
本文介绍了在 Rails 3 中删除 ActiveRecord的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在 Rails 3 测试版已经出来了,我想我应该看看重写一个我刚刚开始在 Rails 3 测试版中工作的应用程序,既要感受一下,又要抢先一步.该应用程序的所有模型都使用 MongoDB 和 MongoMapper,因此不需要 ActiveRecord.在之前的版本中,我是通过以下方式卸载activerecord的:

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:

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*

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

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.yml and replaced it with connection details for MongoDB in order to use this gist for 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....

有人知道正确的 Rails 3 方法吗?

Is anyone aware of the correct Rails 3 way of doing this?

推荐答案

我正在阅读源代码,所以如果它真的有效,请告诉我.:)

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

生成应用程序模板的 rails 命令现在有一个选项 -O,它告诉它跳过 ActiveRecord.

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

如果您不想重新运行 rails,您应该在现有应用中检查以下内容:

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

  • 检查您的 config/application.rb 没有 require 'rails/all'requireactive_record/railtie".相反,对于没有 ActiveRecord 的标准 Rails 设置,它应该满足以下要求:

  • 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

  • 如果在 config/application.rb 中,您使用的是 config.generators 部分,请确保它没有 行g.orm :active_record.如果需要,您可以将其显式设置为 nil,但这应该是完全省略 g.orm 时的默认设置.

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

    可选,但在您的 Gemfile 中,删除加载数据库模块的 gem 行.例如,这可能是 gem "mysql" 行.

    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.

    这篇关于在 Rails 3 中删除 ActiveRecord的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆