Skip to content

Instantly share code, notes, and snippets.

@bturnbull
Created October 12, 2008 01:13
Show Gist options
  • Save bturnbull/16344 to your computer and use it in GitHub Desktop.
Save bturnbull/16344 to your computer and use it in GitHub Desktop.
# First extend ActiveRecord::Base to allow us to override connections
module ActiveRecord
class Base
# Sets the active connection to conn and runs the given block. Active
# connection is reset to it's previous value once the block finishes.
def self.using_connection(conn)
old_connection = use_connection(conn)
ret = yield if block_given?
use_connection(old_connection)
ret
end
# Sets the active connection to conn, returns the previously active connection
def self.use_connection(conn)
old_active_connection = active_connections[active_connection_name]
active_connections[active_connection_name] = conn
old_active_connection
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment