How to set the default config for Mongoid in rails?

62 Views Asked by At

I have set the following config for 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.

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?

0

There are 0 best solutions below