【问题标题】:Can't get mongoid working with Rails 4无法让 mongoid 与 Rails 4 一起工作
【发布时间】:2013-07-21 22:06:48
【问题描述】:

I followed the official tutorial.

我在 Gemfile 中注释掉了 sqlite3 以及以下几行:

gem 'mongoid', '~> 4', github: 'mongoid/mongoid'
gem 'bson_ext'

但是,我一直收到 Specified 'sqlite3' for database adapter, but the gem is not loaded. Add gem "sqlite3" to your Gemfile.

原因似乎是 database.yml 仍然将 sqlite 列为数据库。我应该如何让 Rails 使用生成的 mongoid.yml?用 mongoid.yml 替换 database.yml 的内容似乎没有用 - 我明白了

ActiveRecord::AdapterNotSpecified: database configuration does not specify adapter 错误。

它与 Rails 4 不兼容还是我遗漏了一些简单的东西?

编辑:我想我越来越暖和了。我已将适配器添加为“mongoid”。这是我的 database.yml 现在的内容:

development:
  adapter: 'mongoid'
  # Configure available database sessions. (required)
  sessions:
    # Defines the default session. (required)
    default:
      # Defines the name of the default database that Mongoid can connect to.
      # (required).
      database: xboxie
      # Provides the hosts the default session can connect to. Must be an array
      # of host:port pairs. (required)
      hosts:
        - localhost:27017
      options:
        # Change whether the session persists in safe mode by default.
        # (default: false)
        # safe: false

        # Change the default consistency model to :eventual or :strong.
        # :eventual will send reads to secondaries, :strong sends everything
        # to master. (default: :eventual)
        # consistency: :eventual

        # How many times Moped should attempt to retry an operation after
        # failure. (default: 30)
        # max_retries: 30

        # The time in seconds that Moped should wait before retrying an
        # operation on failure. (default: 1)
        # retry_interval: 1
  # Configure Mongoid specific options. (optional)
  options:
    #
test:
  sessions:
    default:
      database: xboxie_test
      hosts:
        - localhost:27017
      options:
        consistency: :strong
        # In the test environment we lower the retries and retry interval to
        # low amounts for fast failures.
        max_retries: 1
        retry_interval: 0


# # SQLite version 3.x
# #   gem install sqlite3
# #
# #   Ensure the SQLite 3 gem is defined in your Gemfile
# #   gem 'sqlite3'
# development:
#   adapter: sqlite3
#   database: db/development.sqlite3
#   pool: 5
#   timeout: 5000

# # Warning: The database defined as "test" will be erased and
# # re-generated from your development database when you run "rake".
# # Do not set this db to the same as development or production.
# test:
#   adapter: sqlite3
#   database: db/test.sqlite3
#   pool: 5
#   timeout: 5000

# production:
#   adapter: sqlite3
#   database: db/production.sqlite3
#   pool: 5
#   timeout: 5000

产生错误:

LoadError: Could not load 'active_record/connection_adapters/mongoid_adapter'. Make sure that the adapter in config/database.yml is valid. If you use an adapter other than 'mysql', 'mysql2', 'postgresql' or 'sqlite3' add the necessary adapter gem to the Gemfile.

【问题讨论】:

    标签: ruby-on-rails mongodb mongoid ruby-on-rails-4


    【解决方案1】:

    检查您的config/database.yml。您可能在那里指定了 sqlite3。

    【讨论】:

    • 是的,我有。但是,如果我将其更改为 mongoid 我有一个完整的 mongoid.yml 配置文件和一个 database.yml 配置文件,我不知道如何交换这两个
    • 在您链接的教程结束时,他们在谈论如何删除 database.yml 不是吗?
    【解决方案2】:

    我已经通过添加解决了这个问题:

    Mongoid.load!(Rails.root.join("/config/mongoid.yml"))
    

    按照教程发送至config/intializers/mongoid.rb

    您还需要更改 config/application.rb 文件中的以下行:

    require 'rails/all'
    

    到(在 Rails 3.x 中):

    require "action_controller/railtie"
    require "action_mailer/railtie"
    require "active_resource/railtie"
    require "rails/test_unit/railtie"
    # require "sprockets/railtie" # Uncomment this line for Rails 3.1+
    

    或(在 Rails 4.x 中):

    # Pick the frameworks you want:
    # require "active_record/railtie"
    require "action_controller/railtie"
    require "action_mailer/railtie"
    require "sprockets/railtie"
    require "rails/test_unit/railtie"
    

    【讨论】:

      【解决方案3】:

      我也有类似的问题。通过在没有 ActiveRecord 的情况下重新创建我的应用程序解决了所有问题...

      rails new app_name --skip-active-record
      

      如上添加 Gems 并让我的 mongoid.yml 正确...

      【讨论】:

        【解决方案4】:

        使用 mongo 时会出现此问题。基本上 mongo 不适合 active record 。所以使用命令生成你的应用程序,

        rails g myApp --skip-active-record

        在我的情况下效果很好

        【讨论】:

          【解决方案5】:

          就我而言,我收到了来自 puma 的类似错误。然后我意识到在 config/puma.rb 我有:

          on_worker_boot do
            # worker specific setup
            ActiveSupport.on_load(:active_record) do
              config = ActiveRecord::Base.configurations[Rails.env] ||
                  Rails.application.config.database_configuration[Rails.env]
              config['pool'] = ENV['MAX_THREADS'] || 16
              ActiveRecord::Base.establish_connection(config)
            end
          end
          

          我不得不将 on_worker_boot 中的所有内容替换为 config/initializers/mongoid.rb 的内容

          一般来说,我认为调试此问题的一个好方法是在代码中搜索对 active_record 的任何引用。

          【讨论】:

            【解决方案6】:

            我也遇到了同样的错误

            "Could not load active_record/connection_adapters/mongoid_adapter".
            

            我可以通过评论解决它 deveopment.rb 中的“config.active_record.migration_error = :page_load”和 application.rb 中的“config.active_record.raise_in_transactional_callbacks = true”。还要确保在 database.yml 中,数据库不是在 sqlite3 中指定的,而是在 mongoid 中指定的。 您还需要从 application.rb 中删除“require 'rails/all'”并将其替换为

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

            正如上面提到的cmets。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2020-02-29
              • 2011-04-25
              • 2014-11-22
              • 2014-05-15
              • 2010-10-16
              • 2019-11-27
              相关资源
              最近更新 更多