gpt4 book ai didi

How to set the default config for Mongoid in rails?(如何在rails中设置Mongoid的默认配置?)

转载 作者:bug小助手 更新时间:2023-10-22 15:47:02 36 4
gpt4 key购买 nike



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/…

优秀答案推荐
更多回答

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