4

我刚刚开始了一个全新的 Rails 项目,我要完成的第一个任务是创建一个对象并将其保存在我的数据库中。我通过自动生成对象的方式,在本例中为 URL 对象,如下所示:

rails generate scaffold Url domain:string

我还测试了两个单独的 mongoid.yml 配置设置。第一个如下:

development:
  sessions:
    default:
      # Defines the name of the default database that Mongoid can connect to.
      # (required).
      uri: mongodb://[username]:[password]@flame.mongohq.com:27046/[database]

      options:
        consistency: :strong 

此配置的结果是,当发生查询时,我收到以下错误:

The operation: #<Moped::Protocol::Query
  @length=39
  @request_id=5
  @response_to=0
  @op_code=2004
  @flags=[]
  @full_collection_name=".urls"
  @skip=0
  @limit=0
  @selector={}
  @fields=nil>
 failed with error 13075: "db name can't be empty"

我还使用以下配置进行了测试:

development:
  uri: mongodb://[username]:[password]@flame.mongohq.com:27046/[database]

当代码到达 urls_controller 的 create 方法时,它在 @url.save 处失败,并出现以下错误:

mongoid (3.0.0.rc) lib/mongoid/sessions/factory.rb:100:in `parse'
mongoid (3.0.0.rc) lib/mongoid/sessions/factory.rb:61:in `create_session'
mongoid (3.0.0.rc) lib/mongoid/sessions/factory.rb:43:in `default'
mongoid (3.0.0.rc) lib/mongoid/sessions.rb:109:in `default'
mongoid (3.0.0.rc) lib/mongoid/sessions.rb:354:in `__session__'
mongoid (3.0.0.rc) lib/mongoid/sessions.rb:199:in `mongo_session'
mongoid (3.0.0.rc) lib/mongoid/sessions.rb:157:in `collection'
mongoid (3.0.0.rc) lib/mongoid/sessions.rb:25:in `collection'
mongoid (3.0.0.rc) lib/mongoid/persistence/operations.rb:26:in `collection'
mongoid (3.0.0.rc) lib/mongoid/persistence/operations/insert.rb:27:in `block in persist'
mongoid (3.0.0.rc) lib/mongoid/persistence/insertion.rb:25:in `block (2 levels) in  prepare'
activesupport (3.2.3) lib/active_support/callbacks.rb:403:in `_run__2492706777632263523__create__3276423133299307975__callbacks'
activesupport (3.2.3) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.3) lib/active_support/callbacks.rb:385:in `_run_create_callbacks'
activesupport (3.2.3) lib/active_support/callbacks.rb:81:in `run_callbacks'
mongoid (3.0.0.rc) lib/mongoid/callbacks.rb:95:in `run_callbacks'
mongoid (3.0.0.rc) lib/mongoid/persistence/insertion.rb:24:in `block in prepare'
activesupport (3.2.3) lib/active_support/callbacks.rb:403:in  `_run__2492706777632263523__save__3276423133299307975__callbacks'
activesupport (3.2.3) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.3) lib/active_support/callbacks.rb:385:in `_run_save_callbacks'
activesupport (3.2.3) lib/active_support/callbacks.rb:81:in `run_callbacks'
mongoid (3.0.0.rc) lib/mongoid/callbacks.rb:95:in `run_callbacks'
mongoid (3.0.0.rc) lib/mongoid/persistence/insertion.rb:23:in `prepare'
mongoid (3.0.0.rc) lib/mongoid/persistence/operations/insert.rb:26:in `persist'
mongoid (3.0.0.rc) lib/mongoid/persistence.rb:49:in `insert'
mongoid (3.0.0.rc) lib/mongoid/persistence.rb:188:in `upsert'
app/controllers/urls_controller.rb:46:in `block in create'

我的假设是我的 mongoid.yml 文件不正确,但我不确定如何让它工作。有什么想法吗?

4

3 回答 3

6

这就是我的产品 mongoid.yml 的样子

<% if ENV['MONGOLAB_URI'] %>
  <% uri = URI.parse(ENV['MONGOLAB_URI']) %>
production:
  <<: *defaults
  sessions:
    default:
      <<: *default_session
      database: <%= uri.path.sub('/','') %>
      username: <%= uri.user %>
      password: <%= uri.password %>
      hosts:
        - <%= uri.host %>:<%= uri.port %>
<% end %>

这对我有用

于 2012-06-28T14:37:23.310 回答
5

目前在 mongoid 中无法通过 URI 建立数据库连接。mongoid.yml 的格式如下:

environment:
  host: <%= ENV['MONGOID_HOST'] %>
  port: <%= ENV['MONGOID_PORT'] %>
  username: <%= ENV['MONGOID_USERNAME'] %>
  password: <%= ENV['MONGOID_PASSWORD'] %>
  database: <%= ENV['MONGOID_DATABASE'] %>

您需要做的是将 MongoHQ 字符串解析为其组成部分以在 mongoid.yml 中提供

如果你愿意,有人在这里为你编造了一个要点

于 2012-05-29T16:50:09.803 回答
0

对于 MongoHQ 上的副本集/非副本集:

production:
  sessions:
    default:
      <% if ENV['MONGOHQ_HOST_LIST'] %>
      database: <%= ENV['MONGOHQ_DATABASE'] %>
      username: <%= ENV['MONGOHQ_USER'] %>
      password: <%= ENV['MONGOHQ_PASSWD'] %>
      hosts:
      <% YAML.parse(ENV['MONGOHQ_HOST_LIST']).value.each do |v| %>
        - <%= "#{v[0].value}:#{v[1].value}" %>
      <% end %>
      <% elsif ENV['MONGOHQ_URL'] %>
      uri: <%= ENV['MONGOHQ_URL'] %>
      <% end %>
      options:
        consistency: :eventual
        safe: true
于 2012-11-06T15:27:29.853 回答