Search code examples
mongodbruby-on-rails-4mongoidmoped

Mongoid with Rails 4 - Could not connect to a primary node ... resolved_address=nil


I installed MongoDB locally, and am stuck at this error when trying to use Mongoid/Moped on Rails 4 / Ruby 1.9.3 / Windows 7:

"Could not connect to a primary node for replica set #<Moped::Cluster:27191916 @seeds=[<Moped::Node resolved_address=nil>]>"

Probably caused from the resolved_address=nil - other forums stated this is from a typo in the hosts file - any idea where this file is located on Windows?

App contains just a simple model 'Article'

class Article
  include Mongoid::Document
  field :name, type: String
  field :content, type: String
end

and the exception is caught at Articles.all.each.do |article| ...

Running mongod and mongo shell work fine outside of the app- so I'm assuming it's something with the configuration... My Rails server log spits out the following:

Started GET "/articles" for 127.0.0.1 at 2014-01-14 19:04:18 -0800
Processing by ArticlesController#index as HTML
  MOPED: Could not resolve IP for: localhost:27017 runtime: n/a
  MOPED: Retrying connection attempt 1 more time(s). runtime: n/a
  MOPED: Could not resolve IP for: localhost:27017 runtime: n/a
  Rendered articles/index.html.erb within layouts/application (10294.6ms)
Completed 500 Internal Server Error in 10325ms

Far as I can tell, I didn't do anything with replica sets.

Rails.env is development and my config file (mongoid.yml) is:

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

Mongoid may not officially be ready for rails 4 yet - but has anybody had any luck crossing this hurdle?


Solution

  • Review the localhost map in your hosts file or try it:

    development:
      sessions:
        default:
          database: mid_dev
          hosts:
            - 0.0.0.0:27017
    

    Information about Host file: http://en.wikipedia.org/wiki/Hosts_(file)