kilua
kilua

Reputation: 741

Mongoid::Errors::NoSessionConfig in rails consolse

I clone rails app (our company project) and then install everything that related to mongo db and mongoid gem. Then I run the rake db:setup and then rails s. Its working fine, I can access all the sites and do everything. But I got problem in the rails console.

Whenever I do this:

rails c

then

User.first

I will get this error

Loading development environment (Rails 4.1.1) irb(main):001:0> User.first 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. from /Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta2/lib/mongoid/sessions/factory.rb:27:in create' from /Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta2/lib/mongoid/sessions.rb:65:in with_name' from /Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta2/lib/mongoid/sessions.rb:105:in mongo_session' from /Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta2/lib/mongoid/sessions.rb:121:in collection' from /Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta2/lib/mongoid/sessions/options.rb:161:in method_missing' from /Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta2/lib/mongoid/contextual/mongo.rb:263:in initialize' from /Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta2/lib/mongoid/contextual.rb:53:in new' from /Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta2/lib/mongoid/contextual.rb:53:in create_context' from /Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta2/lib/mongoid/contextual.rb:35:in context' from /Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta2/lib/mongoid/contextual.rb:20:in first' from /Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta2/lib/mongoid/findable.rb:122:in first' from (irb):1 from /Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/railties-4.1.1/lib/rails/commands/console.rb:90:in start' from /Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/railties-4.1.1/lib/rails/commands/console.rb:9:in start' from /Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/railties-4.1.1/lib/rails/commands/commands_tasks.rb:69:in console' from /Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/railties-4.1.1/lib/rails/commands/commands_tasks.rb:40:in run_command!' from /Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/railties-4.1.1/lib/rails/commands.rb:17:in ' from /Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:in require' from /Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:inblock in require' from /Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:232:in load_dependency' from /Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:inrequire' from /Users/rizalmuthi/Documents/Sites/WORK/tapway/bin/rails:8:in <top (required)>' from /Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:241:inload' from /Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:241:in block in load' from /Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:232:inload_dependency' from /Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:241:in load' from /Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in require' from /Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in require' from -e:1:in'irb(main):002:0>

And FYI, this is the mongoid.yml that I generated by run

rails g mongoid:config

mongoid.yml

development:
  sessions:
    default:
      database: mongo_demo_development
    hosts:
      - localhost:27017
    options:        
 options:

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

I have been looking all over google and some blogs, could not figure it out how to fix this. Besides that, we dont use the Rspec tho.

Upvotes: 5

Views: 5588

Answers (6)

ThienSuBS
ThienSuBS

Reputation: 1622

With MongoId version 5.x, You will change config in Rails application follow: File: application.rb

# Add line.
config.generators do |g|
  g.orm :mongoid
end

I was set it and it working in rails console. See more at: https://docs.mongodb.com/ecosystem/tutorial/mongoid-installation/#rails-applications

Upvotes: 1

Ferdy
Ferdy

Reputation: 678

In the latest mongoid -5.0.0 version you can simply solve by placing the below code in application.rb

 config.generators do |g|
   g.orm :mongoid
 end

Upvotes: 5

Sushant Mane
Sushant Mane

Reputation: 777

Just make sure that on application initialization you do the following (in Rails console)

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

For more details visit http://mongoid.org/en/mongoid/docs/installation.html

Upvotes: 4

Anthony
Anthony

Reputation: 15957

In config/application.rb change

Bundler.require(*Rails.groups)

to:

Bundler.require(:default, Rails.env)

Rails is trying to eager load the group of gems by environment and the stock config/mongoid.yml file you get when you run rails g mongoid:config only gives you a test and development namespace. Even though you're only running development, it's looking at all the groups. If you add the production namespace to the yml file it will load but my answer above works without you needing to do that.

Upvotes: 4

J Dinesh Reddy
J Dinesh Reddy

Reputation: 1

may be The problem is in the gem file, you did't mention the version of mongoid gem. in gem file change gem "mongoid" to gem "mongoid" , '~> 4.0.2' and run bundle install restart server and console.

Upvotes: 0

Adriano
Adriano

Reputation: 149

change your config/application.rb file:

replace it Bundler.require(*Rails.groups)

to it Bundler.require(*Rails.groups(assets: %w(development test)))

Upvotes: 14

Related Questions