Rails Engine + Mongoid: No configuration could be found for a session named 'default'
Asked Answered
G

6

11

I've created a Rails Mountable App and added 'mongoid' and 'rspec' gem's. If I try to run my specs now I get the following error:

Mongoid::Errors::NoSessionConfig: 
Problem:
  No configuration could be found for a session named 'default'.
Summary:
  When attempting to create the new session, Mongoid could not find a session configuration for the name: 'default'. This is necessary in order to know the host, port, and options needed to connect.
Resolution:
  Double check your mongoid.yml to make sure under the sessions key that a configuration exists for 'default'. If you have set the configuration programatically, ensure that 'default' exists in the configuration hash.

When I add the Mongoid.load!(Rails.root.join("config", "mongoid.yml")) line to spec_helper.rb everything works normal.

Why is that and how can I get the functionality like in a normal Rails app where I don't need to call the load function?

mongoid.yml

development:
  sessions:
    default:
      database: dummy_development
      hosts:
        - localhost:27017
      options:
  options:
test:
  sessions:
    default:
      database: dummy_test
      hosts:
        - localhost:27017
      options:
        consistency: :strong
        max_retries: 1
        retry_interval: 0

Versions:

gem 'rails', '~> 3.2.12'
gem 'mongoid', '~> 3.1'
gem 'rspec-rails', '~> 2.13'
Gardner answered 12/3, 2013 at 6:43 Comment(1)
Can you post your mongoid.yml file?Acotyledon
A
25

you probably missed require 'rails/mongoid' in your spec_helper.rb file.

Had someone having the same issue in here https://github.com/mongoid/mongoid/issues/2894#issuecomment-14903927

Try adding that require, that should fix it.

Acotyledon answered 14/3, 2013 at 20:16 Comment(3)
Where do I find that spec_helper.rb file?Lapoint
I found in the Echo sample project for mongoid+rails, and I didn't have the spec folder in my generated app folderLapoint
spec_helper.rb is generated by rspec, if you're not using rspec, put it on your application.rbAlsacelorraine
M
6

This worked for me on my machine

1: Add this to your config/application.rb

Mongoid.load!("path to your mongoid.yml")

2: And change your mongoid.yml from (Only for mongoid version < 5):

This

development:
  clients:
    default:
      database: database_for_development
        hosts:
          - localhost:27017
test:
  clients:
    default:
      database: database_for_test
        hosts:
          - localhost:27017
production:
  clients:
    default:
      database: database_for_production
        hosts:
          - localhost:27017

To:

development:
  sessions:
    default:
      database: database_for_development
        hosts:
          - localhost:27017
test:
  sessions:
    default:
      database: database_for_test
        hosts:
          - localhost:27017
production:
  sessions:
    default:
      database: database_for_production
        hosts:
          - localhost:27017
Milurd answered 23/8, 2016 at 13:11 Comment(1)
Rails 4.2.7.1 had issue in locating/understanding mongoid.yml file when installing and configuring mongoid. mongoid.yml file was generated successfully but was not recognized. #1 worked for me.Trust
R
4

This is probably because of two simultaneous conditions: ( there is no production section in mongoid.yml ) AND ( Heroku treats Rails applications as production by default ).

Fixing either one shall suffice.

1. There is no production section in mongoid.yml

Add production section to mongoid.yml, as explained at Heroku , e.g.

production:
  sessions:
    default:
      uri: <%= ENV['MONGOHQ_URL'] %>
      options:
        skip_version_check: true
        safe: true

2. Heroku treats rails applications as production by default

Set Heroku environment to development, or a add a new environment which would be specific to Heroku, as explained at Heroku, e.g.

heroku config:set RACK_ENV=development RAILS_ENV=development --remote development
Rossen answered 16/11, 2013 at 15:23 Comment(0)
E
2

And restart the server after making changes to mongoid.yml

Erythrism answered 21/5, 2013 at 0:8 Comment(0)
D
1

I've found this working - notice there is no "sessions", only "clients"

production:
  clients:
    default:
      uri: <%= ENV['MONGODB_URI'] %>
      options:
        skip_version_check: true
        safe: true
Dumanian answered 28/7, 2016 at 14:42 Comment(0)
H
0

Try to regenerate a new mongoid.yml:

rails g mongoid:config

And after change mongoid.yml with your values.

Hydrology answered 23/2, 2020 at 10:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.