I have set the following config for Mongoid.
mongoid.yml
我已经为Mongoid设置了以下配置。mongoid.yml
defaults: &defaults
clients:
default:
hosts:
- <%= ENV.fetch("MONGO_DATABASE_HOST", "localhost") %>:<%= ENV.fetch("MONGO_DATABASE_PORT", "27017").to_i %>
options:
user: <%= ENV.fetch("MONGO_DATABASE_USERNAME", "mongo") %>
password: <%= ENV.fetch("MONGO_DATABASE_PASSWORD", "mongo") %>
max_pool_size: <%= ENV.fetch("RAILS_MAX_THREADS", "5").to_i %>
development:
<<: *defaults
clients:
default:
database: <%= ENV.fetch("DEVELOPMENT_DB_NAME", "development_db") %>
test:
<<: *defaults
clients:
default:
database: <%= ENV.fetch("TEST_DB_NAME", "test_db") %>
options:
read:
mode: :primary
max_pool_size: 1
production:
<<: *defaults
clients:
default:
database: <%= ENV.fetch("PRODUCTION_DATABASE_NAME", "production_db") %>
When I run rails console
, get the following error.
当我运行rails控制台时,会出现以下错误。
There is a configuration error with the current mongoid.yml.
message:
No hosts provided for client configuration: :default.
summary:
Each client configuration must provide hosts so Mongoid knows where the database server is located. What was provided was: {"database"=>"development_db"}.
resolution:
If configuring via a mongoid.yml, ensure that within your :default section a :hosts value for the client hosts is defined.
I think it is for overwriting default
in the development environment.
Can I set the default config?
我认为这是为了在开发环境中覆盖默认值。我可以设置默认配置吗?
更多回答
If this is a modern Rails app you could greatly simplify and improve this by config by using per-environment credentials. You could configure the database name in one single place in the YAML file with database: Rails.application.credentials.mongodb.fetch(:database, "#{Rails.env}_db")
. web-crunch.com/posts/…
如果这是一个现代的Rails应用程序,您可以通过使用每环境凭据进行配置来极大地简化和改进它。您可以使用数据库在YAML文件中的一个位置配置数据库名称:Rails.application.credentials.mongodb.fetch(:database,“#{Rails.env}_db“).webcrunch.com/posts/…
优秀答案推荐
我是一名优秀的程序员,十分优秀!