Rails 5 和 MongoDB:为数据库适配器指定了"sqlite3",但未加载 gem



我正在尝试使用Rails 5设置MongoDB。我已经安装了mongoDB并将其添加到我的宝石文件中。

我运行

rails g mongoid:config

我得到错误:

为数据库适配器指定了" sqlite3",但没有加载该GEM。将gem 'sqlite3'添加到您的gemfile(并确保其版本的最低限度为ActivereCord)

这是database.yml

# SQLite version 3.x
#   gem install sqlite3
#
#   Ensure the SQLite 3 gem is defined in your Gemfile
#   gem 'sqlite3'
#
default: &default
  adapter: sqlite3
  pool: 5
  timeout: 5000
development:
  <<: *default
  database: db/development.sqlite3
# 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:
  <<: *default
  database: db/test.sqlite3
production:
  <<: *default
  database: db/production.sqlite3

我不确定如何为MongoDB重写它?我认为我还需要删除或禁用ActivereCord,但我不确定如何在Rails 5中执行此操作。

删除生成的应用程序,然后使用--skip-active-record选项生成新的Rails应用程序。

rails new your-project --skip-active-record

然后,将Mongo适配器添加到您的Gemfile等等。

您可以做到这一点:

database.yml

development: &global_settings
  database: textual_development
  host: 127.0.0.1
  port: 27017
test:
  database: textual_test
  <<: *global_settings
production:
  host: hostname
  database: databasename
  username: username
  password: password
  <<: *global_settings

然后在config/initializer中创建一个名为mongo.rb

的初始化器

mongo.rb

MongoMapper.setup(Rails.configuration.database_configuration, Rails.env, :logger => Rails.logger)

享受!