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

时间:2017-03-13 07:17:26

标签: ruby-on-rails ruby mongodb ruby-on-rails-5 cloud9-ide

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

当我跑步时

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中执行此操作。

2 个答案:

答案 0 :(得分:1)

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

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

然后将mongo适配器添加到Gemfile中,依此类推。

答案 1 :(得分:0)

你可以这样做:

的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)

享受!