1

I have been learning Ruby on Rails lately and was using Heroku as a free production environment for learning. I got to the point I wanted to add MongoDB to my setup. I chose Mongoid 5 (the latest version right now) and MongoLab - which offers a free plan on Heroku.

I got stumped configuring mongoid.yml because most of the answers I found on stackoverflow are outdated - talking about version 4, 3 or even 2.

So after I found a solution that works, I decided to post it here, mostly as a way of documenting it. If you have a better way to configure mongoid.yml to work with MongoLab on Heroku, it will be more than welcome.

Thanks.

kostia
  • 6,161
  • 3
  • 19
  • 23

1 Answers1

0

While looking for this answer, I found either solutions for older versions of Mongoid or solutions that work with MongoHQ (the other Heroku mongoDB hosting solution).

I ended up using this answer which demonstrated a way to do it with Mongoid 4 for MongoHQ. I adapted it to work with Mongoid 5 and MongoLab and ended up with the following:

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

Hope that helps you.

Community
  • 1
  • 1
kostia
  • 6,161
  • 3
  • 19
  • 23