Module: ActiveRecord::Connections
- Defined in:
- lib/active_record/connections.rb,
lib/active_record/connections/connection_proxy.rb
Defined Under Namespace
Classes: ConnectionProxy
Instance Method Summary collapse
- #proxy_connection ⇒ Object
- #proxy_connection=(proxy_connection) ⇒ Object
-
#using_connection(connection_name, connection_spec) ⇒ Object
Using on ApplicationController:.
Instance Method Details
#proxy_connection ⇒ Object
52 53 54 |
# File 'lib/active_record/connections.rb', line 52 def proxy_connection Thread.current["ActiveRecord::Connections.proxy_connection"] end |
#proxy_connection=(proxy_connection) ⇒ Object
56 57 58 |
# File 'lib/active_record/connections.rb', line 56 def proxy_connection=(proxy_connection) Thread.current["ActiveRecord::Connections.proxy_connection"] = proxy_connection end |
#using_connection(connection_name, connection_spec) ⇒ Object
Using on ApplicationController:
class ApplicationController < ActionController::Base
before_filter :handle_customer
protected
def handle_customer(&block)
customer = Customer.find_by_domain!(request.domain)
ActiveRecord::Base.using_connection(customer.id, customer.connection_spec, &block)
end
end
Using directly on models:
customer = Customer.first
ActiveRecord::Base.using_connection(customer.id, customer.connection_spec) do
User.count # => 3
end
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/active_record/connections.rb', line 28 def using_connection(connection_name, connection_spec) self.proxy_connection = ConnectionProxy.new(connection_name, connection_spec) def self.connection_pool connection_handler.retrieve_connection_pool(proxy_connection) end def self.retrieve_connection connection_handler.retrieve_connection(proxy_connection) end yield ensure self.proxy_connection = nil def self.connection_pool connection_handler.retrieve_connection_pool(self) end def self.retrieve_connection connection_handler.retrieve_connection(self) end end |