method
connects_to
v6.1.3.1 -
Show latest stable
-
0 notes -
Class: ConnectionHandling
- 1.0.0
- 1.1.6
- 1.2.6
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.8
- 3.0.0
- 3.0.9
- 3.1.0
- 3.2.1
- 3.2.8
- 3.2.13
- 4.0.2
- 4.1.8
- 4.2.1
- 4.2.7
- 4.2.9
- 5.0.0.1
- 5.1.7
- 5.2.3
- 6.0.0 (0)
- 6.1.3.1 (38)
- 6.1.7.7 (0)
- 7.0.0 (0)
- 7.1.3.2 (-5)
- 7.1.3.4 (0)
- What's this?
connects_to(database: {}, shards: {})
public
Connects a model to the databases specified. The database keyword takes a hash consisting of a role and a database_key.
This will create a connection handler for switching between connections, look up the config hash using the database_key and finally establishes a connection to that config.
class AnimalsModel < ApplicationRecord self.abstract_class = true connects_to database: { writing: :primary, reading: :primary_replica } end
connects_to also supports horizontal sharding. The horizontal sharding API also supports read replicas. Connect a model to a list of shards like this:
class AnimalsModel < ApplicationRecord self.abstract_class = true connects_to shards: { default: { writing: :primary, reading: :primary_replica }, shard_two: { writing: :primary_shard_two, reading: :primary_shard_replica_two } } end
Returns an array of database connections.