无法让mongoid使用Rails 4

时间:2013-07-23 01:16:36

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

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.

6 个答案:

答案 0 :(得分:12)

我通过添加:

解决了这个问题
Mongoid.load!(Rails.root.join("/config/mongoid.yml"))

config/intializers/mongoid.rb,根据教程。

此外,您还需要在以下位置更改config / application.rb文件中的以下行:

require 'rails/all'

to(在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"

答案 1 :(得分:8)

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

rails new app_name --skip-active-record

如上所述添加Gems并使我的mongoid.yml正确...

答案 2 :(得分:2)

使用mongo时会发生此问题。基本上mongo与积极的记录并不顺利。因此,使用命令

生成应用程序

rails g myApp --skip-active-record

在我的案例中运作良好

答案 3 :(得分:1)

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

答案 4 :(得分:0)

在我的情况下,我收到了来自美洲狮的类似错误。我意识到在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

我必须使用config / initializers / mongoid.rb的内容替换on_worker_boot中的所有内容

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

答案 5 :(得分:0)

我也遇到了与

相同的错误
"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'trail / all'”并将其替换为

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

如上述评论所述。