title: Establish a connection to another database only in a block?
body: <p>In a rails application, I have this code in pure ruby :</p>
  
  <pre><code>class LinkCreator
    attr_accessor :animal
  
    def initialize(animal:)
      @animal = animal
    end
  
    def call
      "something#{link_id}"
    end
  
    private
  
    def link_id
      connection.execute(sql_request).first.first
    end
  
    def sql_request
      "SELECT field FROM table WHERE field_id = '#{field_id}' LIMIT 1"
    end
  
    def field_id
      animal.field_id
    end
  
    def connection
      ActiveRecord::Base.establish_connection(
        adapter:  "mysql",
        host:     ENV["MYSQL_HOST"],
        username: ENV["MYSQL_USERNAME"],
        password: ENV["MYSQL_PASSWORD"],
        database: ENV["MYSQL_DB_NAME"]
      ).connection
    end
  end
  </code></pre>
  
  <p>As you can see, this is not a model but only a simple class. The problem is than the connection of activerecord is changed and the other requests, later, are executed on the new connection.</p>
  
  <p>Is it possible to establish a connection only in a block and go back to the old connection. I know I can establish another connection but this is very bad for performance.</p>