title: Sinatra, Mongoid, Heroku, MongoHQ: connecting to Mongodb
body: <p>Trying to get Mongoid up and running with Sinatra on Heroku (MongoHQ). Previous experience with Rails but first time with the stack and Sinatra.</p>
  
  <p>Started with one of the simple examples on the web (<code>app.rb</code>):</p>
  
  <pre><code>require 'rubygems'
  require 'sinatra'
  require 'mongo'
  require 'mongoid'
  
  configure do
    Mongoid.load!('mongoid.yml')
  
    Mongoid.configure do |config|
      if ENV['MONGOHQ_URL']
        conn = Mongo::Connection.from_uri(ENV['MONGOHQ_URL'])
        uri = URI.parse(ENV['MONGOHQ_URL'])
  
        # problem happens here 
        config.master = conn.db(uri.path.gsub(/^\//, ''))
      else
        config.master = Mongo::Connection.from_uri("mongodb://localhost:27017").db('test')
      end
    end
  end
  
  
  # Models
  class Counter
    include Mongoid::Document
  
    field :count, :type =&gt; Integer
  
    def self.increment
      c = first || new({:count =&gt; 0})
      c.inc(:count, 1)
      c.save
      c.count
    end
  end
  
  # Controllers
  get '/' do
    "Hello visitor n" + Counter.increment.to_s
  end
  </code></pre>
  
  <p>For reference, <code>mongoid.yml</code> looks like:</p>
  
  <pre><code>development:
    sessions:
      default:
        database: localhost
  production:
    sessions:
      default:
        uri: &lt;%= ENV['MONGOHQ_URL'] %&gt;
  </code></pre>
  
  <p>As per <code>app.rb (# problem happens here)</code>, my logs say:</p>
  
  <pre><code>/app/app.rb:15:in `block (2 levels) in &lt;top (required)&gt;': undefined method `master=' for Mongoid::Config:Module (NoMethodError)
      from /app/vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.3/lib/mongoid.rb:112:in `configure'
  from /app/app.rb:11:in `block in &lt;top (required)&gt;'
  from /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.2/lib/sinatra/base.rb:1273:in `configure'
  from /app/app.rb:8:in `&lt;top (required)&gt;'
  </code></pre>
  
  <p>I have also tried variants, including:</p>
  
  <pre><code>config.master = Mongo::Connection.from_uri(ENV['MONGOHQ_URL']).db('appXXXXX')
  Mongoid.database = Mongo::Connection.from_uri(ENV['MONGOHQ_URL']).db('appXXXXXXX')
  </code></pre>
  
  <p>But get the same error:</p>
  
  <pre><code>undefined method `master` for Mongoid::Config:Module (NoMethodError)
  </code></pre>
  
  <p>or:</p>
  
  <pre><code>undefined method `database=` for Mongoid::Config:Module (NoMethodError)
  </code></pre>
  
  <p>What am I missing?</p>