4

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.

Stennie
  • 63,885
  • 14
  • 149
  • 175
kilua
  • 711
  • 1
  • 9
  • 16
  • May be you should look at a [similar problem](http://stackoverflow.com/questions/15354936/rails-engine-mongoid-no-configuration-could-be-found-for-a-session-named-def) here – Robert Christopher Aug 11 '14 at 04:15
  • Hi @RobertChristopher As I mentioned, I am not using rspec, so that link wont help much. Any other idea? Thanks – kilua Aug 11 '14 at 06:46
  • @kulia try starting console in production environment `rails c production` after inserting a production section in your mongoid.yml – Robert Christopher Aug 11 '14 at 07:03
  • @RobertChristopher I got problem on development, not production yet – kilua Aug 11 '14 at 09:43
  • @kulia I believe you have to provide more information here, since your question also mentions about running `rake db:setup` usually these commands do not work in NoSQL databases (MongoDB) – Robert Christopher Aug 11 '14 at 10:04
  • @RobertChristopher so you are saying, I dont need to run the `rake db:setup` let me try again and come back. But I think, that is not the matter. Did I miss something with the gem or mongoDb setup? – kilua Aug 11 '14 at 15:53
  • Just in case, have a look at this [question](http://stackoverflow.com/questions/17800000/cant-get-mongoid-working-with-rails-4?rq=1) – Robert Christopher Aug 11 '14 at 16:23
  • I have simmilar issue in production, try with `RAILS_ENV=production rails c` instead `rails c -e production` – rails_id Mar 07 '18 at 02:12

6 Answers6

14

change your config/application.rb file:

replace it Bundler.require(*Rails.groups)

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

Adriano
  • 149
  • 2
5

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
Sergey Kishenin
  • 5,099
  • 3
  • 30
  • 50
Ferdy
  • 666
  • 7
  • 25
4

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

Sushant Mane
  • 757
  • 7
  • 14
  • It works but as a rails newbie i think we miss something... the 'rails c' should throw you into the same environment which comes handy to see the same behavior and problems of the rails application. if you inject the yml directly into the irb, u may see different behavior that the application will not see ... is it correct ? – chenchuk Oct 15 '18 at 14:59
4

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.

Anthony
  • 15,435
  • 4
  • 39
  • 69
1

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

ThienSuBS
  • 1,574
  • 18
  • 26
0

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.