method

connects_to

Importance_2
v7.1.3.2 - Show latest stable - 0 notes - Class: ConnectionHandling
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 look up the database config using the database_key and establish 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 supports read replicas as well. You can 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.

Show source
Register or log in to add new notes.