Question

I'm using Heroku + Ruby 2.0.0p247, Rails 4.0.0, Mongoid4.0.0, MongoHQ add-on

What have I done:

created new project using latest rails and ruby without activeRecords, installed gems mongoid and bson_ext, created config file for mongoid:

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


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

, created new model :

class Phone
  include Mongoid::Document
  field :name, type: String
  field :photo, type: String
end

.

Now when I'm trying to do something like that(in console or controller):

phones = Phone.all
phones.each do |phone|
  p phone.name
end

, I'm getting this error:

RuntimeError: No database set for session. Call #use or #with before accessing the database
    from /app/vendor/bundle/ruby/2.0.0/gems/moped-2.0.0.beta2/lib/moped/session.rb:410:in `current_database'
    from /app/vendor/bundle/ruby/2.0.0/gems/moped-2.0.0.beta2/lib/moped/session.rb:50:in `[]'
    from /app/vendor/bundle/ruby/2.0.0/bundler/gems/mongoid-cc7a0e709066/lib/mongoid/sessions.rb:121:in `collection'
    from /app/vendor/bundle/ruby/2.0.0/bundler/gems/mongoid-cc7a0e709066/lib/mongoid/sessions/options.rb:157:in `method_missing'
    from /app/vendor/bundle/ruby/2.0.0/bundler/gems/mongoid-cc7a0e709066/lib/mongoid/contextual/mongo.rb:265:in `initialize'
    from /app/vendor/bundle/ruby/2.0.0/bundler/gems/mongoid-cc7a0e709066/lib/mongoid/contextual.rb:51:in `new'
    from /app/vendor/bundle/ruby/2.0.0/bundler/gems/mongoid-cc7a0e709066/lib/mongoid/contextual.rb:51:in `create_context'
    from /app/vendor/bundle/ruby/2.0.0/bundler/gems/mongoid-cc7a0e709066/lib/mongoid/contextual.rb:34:in `context'
    from /app/vendor/bundle/ruby/2.0.0/bundler/gems/mongoid-cc7a0e709066/lib/mongoid/contextual.rb:18:in `each'
    from (irb):5
    from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in `start'
    from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in `start'
    from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.0.0/lib/rails/commands.rb:64:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'

Have no idea how to fix it, googled all day long and no result :(

In my Gemfile:

gem 'rails', '4.0.0'
gem 'mongoid', git: 'https://github.com/mongoid/mongoid.git'
gem "bson_ext"

I haven't tried to launch it on localhost, because I have no mongoDB installed.

New information! After launching it:

ph = Phone.new(name:'qwe',photo:'qweqw')
ph.save

I'm getting error:

Moped::Errors::OperationFailure: The operation: #<Moped::Protocol::Command
  @length=64
  @request_id=5
  @response_to=0
  @op_code=2004
  @flags=[]
  @full_collection_name=".$cmd"
  @skip=0
  @limit=-1
  @selector={:getlasterror=>1, :w=>1}
  @fields=nil>
failed with error 16256: "Invalid ns [.$cmd]"

See https://github.com/mongodb/mongo/blob/master/docs/errors.md
for details about this error.
    from /app/vendor/bundle/ruby/2.0.0/gems/moped-2.0.0.beta2/lib/moped/operation/read.rb:54:in `block in execute'
    from /app/vendor/bundle/ruby/2.0.0/gems/moped-2.0.0.beta2/lib/moped/node.rb:584:in `[]'
    from /app/vendor/bundle/ruby/2.0.0/gems/moped-2.0.0.beta2/lib/moped/node.rb:584:in `block (3 levels) in flush'
    from /app/vendor/bundle/ruby/2.0.0/gems/moped-2.0.0.beta2/lib/moped/node.rb:583:in `map'
    from /app/vendor/bundle/ruby/2.0.0/gems/moped-2.0.0.beta2/lib/moped/node.rb:583:in `block (2 levels) in flush'
    from /app/vendor/bundle/ruby/2.0.0/gems/moped-2.0.0.beta2/lib/moped/node.rb:192:in `block in ensure_connected'
    from /app/vendor/bundle/ruby/2.0.0/gems/moped-2.0.0.beta2/lib/moped/executable.rb:25:in `execute'
    from /app/vendor/bundle/ruby/2.0.0/gems/moped-2.0.0.beta2/lib/moped/node.rb:189:in `ensure_connected'
    from /app/vendor/bundle/ruby/2.0.0/gems/moped-2.0.0.beta2/lib/moped/node.rb:577:in `block in flush'
    from /app/vendor/bundle/ruby/2.0.0/gems/moped-2.0.0.beta2/lib/moped/node.rb:608:in `block in logging'
    from /app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.0/lib/active_support/notifications.rb:159:in `block in instrument'
    from /app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.0/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
    from /app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.0/lib/active_support/notifications.rb:159:in `instrument'
    from /app/vendor/bundle/ruby/2.0.0/gems/moped-2.0.0.beta2/lib/moped/instrumentable.rb:31:in `instrument'
    from /app/vendor/bundle/ruby/2.0.0/gems/moped-2.0.0.beta2/lib/moped/node.rb:607:in `logging'
    from /app/vendor/bundle/ruby/2.0.0/gems/moped-2.0.0.beta2/lib/moped/node.rb:576:in `flush'
... 14 levels...
    from /app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:373:in `_run__1088715116367060670__create__callbacks'
    from /app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:80:in `run_callbacks'
    from /app/vendor/bundle/ruby/2.0.0/bundler/gems/mongoid-cc7a0e709066/lib/mongoid/interceptable.rb:132:in `run_callbacks'
    from /app/vendor/bundle/ruby/2.0.0/bundler/gems/mongoid-cc7a0e709066/lib/mongoid/persistable/creatable.rb:117:in `block in prepare_insert'
    from /app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:373:in `_run__1088715116367060670__save__callbacks'
    from /app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:80:in `run_callbacks'
    from /app/vendor/bundle/ruby/2.0.0/bundler/gems/mongoid-cc7a0e709066/lib/mongoid/interceptable.rb:132:in `run_callbacks'
    from /app/vendor/bundle/ruby/2.0.0/bundler/gems/mongoid-cc7a0e709066/lib/mongoid/persistable/creatable.rb:116:in `prepare_insert'
    from /app/vendor/bundle/ruby/2.0.0/bundler/gems/mongoid-cc7a0e709066/lib/mongoid/persistable/creatable.rb:23:in `insert'
    from /app/vendor/bundle/ruby/2.0.0/bundler/gems/mongoid-cc7a0e709066/lib/mongoid/persistable/savable.rb:23:in `save'
    from (irb):18
    from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in `start'
    from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in `start'
    from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.0.0/lib/rails/commands.rb:64:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'
Was it helpful?

Solution

So, first of all you only need to install mongoid gem. bson_ext is not necessary anymore. Second, there was a bug on the commit that you are using, related to database config. If you probably bundle update mongoid to get the latest version you will get a fix for that. Please try that out, and let me know.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top