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

Rails 5 and MongoDB: Specified 'sqlite3' for database adapter, but the gem is not loaded

我正在尝试设置 MongoDB 和 Rails 5。我正在使用 Cloud9,它似乎会自动设置您使用 SQLite,所以我遇到了一些问题。我已经安装了 MongoDB 并将其添加到我的 gem 文件中。

当我运行

rails g mongoid:config

我收到错误:

Specified 'sqlite3' for database adapter, but the gem is not loaded. Add gem 'sqlite3' to your Gemfile (and ensure its version is at the minimum required by 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.

中执行此操作

你可以这样做:

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/initializers 中创建一个名为 mongo.rb

的初始化程序

mongo.rb

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

尽情享受吧!

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

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

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