Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3440080
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:23:22+00:00 2026-05-18T08:23:22+00:00

Using the ruby mongodb driver, is there a way I can create a connection

  • 0

Using the ruby mongodb driver, is there a way I can create a connection object in the configure block that can be accessed in the route methods so that I don’t have to recreate the connection on each request?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T08:23:22+00:00Added an answer on May 18, 2026 at 8:23 am

    Set a global variable in a configuration block:

    configure do
      $mongo = Mongo::Connection.new
    end
    

    or stick it in settings:

    configure do
      set :mongo, Mongo::Connection.new
    end
    
    get '/' do
      # the connection is available through settings.mongo
    end
    

    I must say that I find neither of these very elegant.

    When developing in may look as if the connection is created on every request, but start your server in production and you will see that it behaves differently (for example, thin -e production).

    Also, if your app will run under Passenger, you need to do this:

     configure do
       if defined?(PhusionPassenger)
         PhusionPassenger.on_event(:starting_worker_process) do |forked|
           if forked
             # *** reconnect to the database here! ***
           end
         end
       end
     end
    

    What it does is that it reconnects to the database after Passenger forks, so that child processes have their own connection. Not doing this will give you really strange errors.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a Ruby on Rails app that is using MongoDB for datastorage. Mongoid::Timestamp
How can I use MapReduce in Ruby on Rails if I'm using the MongoDB
(using Ruby on Rails and ActiveRecord) In short: Is there some way how a
related to MongoDB Group using Ruby driver if I want to do something like
I wonder if the ruby driver for MongoDB is using the native javascript driver,
I was trying to use the count() Mongodb feature (db.collection_name.count({data:value}) using the Ruby Driver.
I have been using MongoDB and the Ruby driver and Mongoid, and lines db.things.find({j:
How can I collect real-time tweets using Ruby and store them in MongoDB? Please
Imagine I have this object (written with Ruby literals) stored in a MongoDB: {tags
I am developing a facebook application in Ruby on Rails using MongoDB as database.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.