0

Heroku + Ruby 2.0.0p247、Rails 4.0.0、Mongoid4.0.0、MongoHQ アドオンを使用しています

私が何をした:

ActiveRecords なしで最新の rails と ruby​​ を使用して新しいプロジェクトを作成し、gem の mongoid と bson_ext をインストールし、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

、新しいモデルを作成しました:

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

.

今、私がそのようなことをしようとしているとき(コンソールまたはコントローラーで):

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

、次のエラーが表示されます。

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>'

それを修正する方法がわかりません。一日中グーグル検索しても結果はありません:(

私のGemfileで:

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

mongoDB がインストールされていないため、localhost で起動しようとはしていません。

新情報!起動後:

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

エラーが発生します:

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>'
4

1 に答える 1