instance method
connects_to
Ruby on Rails 6.0.6
Since v6.0.6Signature
connects_to(database: {})
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
Returns an array of established connections.
Parameters
-
databasekey = {}
Source
# File activerecord/lib/active_record/connection_handling.rb, line 68
def connects_to(database: {})
connections = []
database.each do |role, database_key|
config_hash = resolve_config_for_connection(database_key)
handler = lookup_connection_handler(role.to_sym)
connections << handler.establish_connection(config_hash)
end
connections
end
Defined in activerecord/lib/active_record/connection_handling.rb line 68
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveRecord::ConnectionHandling