gpt4 book ai didi

ruby-on-rails - 无法让 mongoid 与 Rails 4 一起工作

转载 作者:IT老高 更新时间:2023-10-28 13:11:21 28 4
gpt4 key购买 nike

I followed the official tutorial .

我在 Gemfile 中注释掉了 sqlite3 以及以下几行:

gem 'mongoid', '~> 4', github: 'mongoid/mongoid'
gem 'bson_ext'

但是,我一直收到为数据库适配器指定了“sqlite3”,但未加载 gem。将 gem "sqlite3"添加到您的 Gemfile。

原因似乎是 database.yml 仍然将 sqlite 列为数据库。我应该如何让 Rails 使用生成的 mongoid.yml?用 mongoid.yml 替换 database.yml 的内容似乎没有用 - 我明白了

ActiveRecord::AdapterNotSpecified: 数据库配置未指定适配器错误。

它与 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: 无法加载“active_record/connection_adapters/mongoid_adapter”。确保 config/database.yml 中的适配器有效。如果您使用“mysql”、“mysql2”、“postgresql”或“sqlite3”以外的适配器,请将必要的适配器 gem 添加到 Gemfile。

最佳答案

我已经通过添加解决了这个问题:

Mongoid.load!(Rails.root.join("/config/mongoid.yml"))

config/initializers/mongoid.rb,按照教程。

您还需要更改 config/application.rb 文件中的以下行:

require 'rails/all'

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

关于ruby-on-rails - 无法让 mongoid 与 Rails 4 一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17800000/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com