Module: Slaver::Connection::ClassMethods
- Defined in:
- lib/slaver/connection.rb
Instance Method Summary collapse
-
#on(config_name) ⇒ Object
Public: Change database connection for next query WARNING: It’ll change current DB connection until insert, select or execute methods call.
-
#within(config_name) ⇒ Object
Public: Changes model’s connection to database temporarily to execute block.
Instance Method Details
#on(config_name) ⇒ Object
Public: Change database connection for next query WARNING: It’ll change current DB connection until insert, select or execute methods call
config_name - String or Symbol, name of config_section
NOTE:
if config was not found:
1) On production
throws ArgumentError
2) Uses default configuration for current environment
Exception safety:
throws ArgumentError if no configuration was found
Examples
SomeModel.on(:slave).create(…) SomeModel.on(:slave).where(…).first
It also can be chained with other ‘on’ methods SomeModel.on(:slave).on(:other).find_by(…)
Returns self
39 40 41 |
# File 'lib/slaver/connection.rb', line 39 def on(config_name) ScopeProxy.new(self, config_name) end |
#within(config_name) ⇒ Object
Public: Changes model’s connection to database temporarily to execute block.
config_name - String or Symbol, name of config_section
NOTE:
if config was not found:
1) On production
throws ArgumentError
2) Uses default configuration for current environment
Exception safety:
throws ArgumentError if no configuration was found
Examples
SomeModel.within :test_slave do
# do some computations here
end
It is also possible to nest database connection code
SomeModel.within :slave do
do some computations here
SomeModel.within :slave2 do
# some other computations go here
end
end
Returns noting
72 73 74 |
# File 'lib/slaver/connection.rb', line 72 def within(config_name) config_handler.run_with(self, config_name, pools_handler) { yield } end |