无法使Moongoid与Rails 4一起使用 [英] Can't get mongoid working with Rails 4
问题描述
我已在我的Gemfile中注释了sqlite3以及以下几行:
I have sqlite3 commented out in my Gemfile as well as the following lines:
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的内容似乎不起作用-我得到了
Reason seems to be that the database.yml still lists sqlite as the database. How am I supposed to get Rails to use the generated mongoid.yml? Replacing database.yml's contents with mongoid.yml doesn't seem to do the trick - I get the
ActiveRecord::AdapterNotSpecified: database configuration does not specify adapter
错误.
它与Rails 4不兼容,还是我缺少简单的东西?
Is it not compatible with Rails 4 or am I missing something simple?
我想我正在变暖.我已将适配器添加为"mongoid".这是我的database.yml的内容:
I think I'm getting warmer. I've added the adapter as 'mongoid'. Here's the contents of my database.yml now:
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.
推荐答案
我已经通过添加以下内容解决了此问题:
I've solved this by adding:
Mongoid.load!(Rails.root.join("/config/mongoid.yml"))
从
到config/intializers/mongoid.rb
,按照本教程.
此外,您还需要从以下位置更改config/application.rb文件中的以下行:
Also you'll need to change the following line in your config/application.rb file from:
require 'rails/all'
到(在Rails 3.x中):
to (in 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中):
or (in 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"
这篇关于无法使Moongoid与Rails 4一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!