Skip to content

Instantly share code, notes, and snippets.

@mranallo
Created February 7, 2012 15:46
Show Gist options
  • Save mranallo/1760327 to your computer and use it in GitHub Desktop.
Save mranallo/1760327 to your computer and use it in GitHub Desktop.
Mongo Connection Id
module Mongodb
def self.db
config = Rails.configuration.database_configuration["mongodb_#{Rails.env}"]
host = config["host"]
database = config["database"]
username = config["username"]
password = config["password"]
port = config["port"]
if Rails.env == "development"
@@connection ||= Mongo::Connection.new host, port, :logger => Rails.logger, :safe => true
else
host.each { |h| h[1] << port }
@@connection ||= Mongo::ReplSetConnection.new(host["primary"], host["secondary"], :logger => Rails.logger, :refresh_mode => :sync)
end
@@db ||= @@connection[database]
unless username.nil? || password.nil?
@@db.authenticate(username, password)
end
@@db
end
def self.sent_email
@collection ||= Mongodb.db['sent_email']
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment