<p>I'm writing a Rails application which will monitor data quality over some specific databases. In order to do that, I need to be able to execute direct SQL queries over these databases - which of course are not the same as the one used to drive the Rails application models. In short, this means I can't use the trick of going through the ActiveRecord base connection.</p>

<p>The databases I need to connect to are not known at design time (i.e.: I can't put their details in database.yaml). Rather, I have a model 'database_details' which the user will use to enter the details of the databases over which the application will execute queries at runtime. </p>

<p>So the connection to these databases really is dynamic and the details are resolved at runtime only.</p>

[{'answer_id': 80946, 'author': 'Codebeef', 'author_id': 12037, 'author_profile': 'https://Stackoverflow.com/users/12037', 'pm_score': 0, 'selected': False, 'text': '<p>You may be able to do this through <a href="http://blog.whitet.net/articles/2008/01/30/rails-has_many-with-models-over-different-databases" rel="nofollow noreferrer"><code>self.establish_connection</code></a>.</p>\n'}, {'answer_id': 81207, 'author': 'Jean', 'author_id': 7898, 'author_profile': 'https://Stackoverflow.com/users/7898', 'pm_score': 3, 'selected': False, 'text': '<p>You can programmatically establish a connection using a call like this </p>\n\n<pre><code>ActiveRecord::Base.establish_connection(\n   :adapter  =&gt; "mysql",\n   :host     =&gt; "localhost",\n   :username =&gt; "myuser",\n   :password =&gt; "mypass",\n   :database =&gt; "somedatabase"\n)\n</code></pre>\n\n<p>As you see you can replace the somedatabase by a <code>database_model.database_name</code> value. The same is true with the adapter and all.</p>\n\n<p>See <a href="http://api.rubyonrails.com/classes/ActiveRecord/Base.html#M001397" rel="nofollow noreferrer"><code>ActiveRecord::Base.establish_connection</code> documentation</a> for more information.</p>\n\n<p>Then you can use: </p>\n\n<pre><code>ActiveRecord::Base.find_by_sql("select * ") \n</code></pre>\n\n<p>to execute your SQL query. </p>\n\n<p>See <a href="http://api.rubyonrails.com/classes/ActiveRecord/Base.html#M001302" rel="nofollow noreferrer"><code>ActiveRecord::Base.find_by_sql</code> documentation</a> for more information.</p>\n\n<p><a href="https://stackoverflow.com/users/12037/mr-matt">Mr Matt</a> was right if incomplete.</p>\n\n<p>More information, which is outdated but still useful for the design approach, can be found <a href="http://wiki.rubyonrails.org/rails/pages/HowtoUseMultipleDatabases" rel="nofollow noreferrer">here</a> and remember to reconnect to the normal database when you are done.</p>\n'}, {'answer_id': 3737505, 'author': 'brokenbeatnik', 'author_id': 90709, 'author_profile': 'https://Stackoverflow.com/users/90709', 'pm_score': 4, 'selected': True, 'text': '<p>I had a situation like this where I had to connect to hundreds of different instances of an external application, and I did code similar to the following:</p>\n\n<pre><code>  def get_custom_connection(identifier, host, port, dbname, dbuser, password)\n      eval("Custom_#{identifier} = Class::new(ActiveRecord::Base)")\n      eval("Custom_#{identifier}.establish_connection(:adapter=&gt;\'mysql\', :host=&gt;\'#{host}\', :port=&gt;#{port}, :database=&gt;\'#{dbname}\', " +\n      ":username=&gt;\'#{dbuser}\', :password=&gt;\'#{password}\')")  \n    return eval("Custom_#{identifier}.connection")\n  end\n</code></pre>\n\n<p>This has the added benefit of not changing your ActiveRecord::Base connection that your models inherit from, so you can run SQL against this connection and discard the object when you\'re done with it.</p>\n'}]
['https://Stackoverflow.com/questions/80859', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/11477/']