retrieve_connection(spec_name, role: ActiveRecord::Base.current_role, shard: ActiveRecord::Base.current_shard)
public
Locate the connection of the nearest super class. This can be an active or
defined connection: if it is the latter, it will be opened and set as the
active connection for the class it was defined for (not necessarily the
current class).
Show source
def retrieve_connection(spec_name, role: ActiveRecord::Base.current_role, shard: ActiveRecord::Base.current_shard)
pool = retrieve_connection_pool(spec_name, role: role, shard: shard)
unless pool
if shard != ActiveRecord::Base.default_shard
message = "No connection pool for '#{spec_name}' found for the '#{shard}' shard."
elsif ActiveRecord::Base.connection_handler != ActiveRecord::Base.default_connection_handler
message = "No connection pool for '#{spec_name}' found for the '#{ActiveRecord::Base.current_role}' role."
elsif role != ActiveRecord::Base.default_role
message = "No connection pool for '#{spec_name}' found for the '#{role}' role."
else
message = "No connection pool for '#{spec_name}' found."
end
raise ConnectionNotEstablished, message
end
pool.connection
end