0

我正在使用 Heroku + Ruby 2.0.0p247、Rails 4.0.0、Mongoid4.0.0、MongoHQ 插件

我做了什么:

使用没有 activeRecords 的最新 rails 和 ruby​​ 创建了新项目,安装了 gems 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"

我没有尝试在 localhost 上启动它,因为我没有安装 mongoDB。

新的信息!启动后:

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

0

所以,首先你只需要安装mongoidgem。bson_ext不再需要了。其次,您正在使用的提交存在与数据库配置相关的错误。如果您可能bundle update mongoid要获得最新版本,您将获得修复。请尝试一下,然后告诉我。

于 2013-10-11T14:36:19.150 回答