How can I connect to another DB for a specific method in the class?

Due to the system administrator, I need to make a request in our application against the subordinate database compared to the production database.

I can use connection_connection at the class level, but I do not want to violate other class methods. Therefore, I wonder how I can create a database connection for this particular class method without having to point all my methods to the slave db?

How do you do it in Perl or Php.

Here is an example: http://pastie.org/private/0k8xqssjrib94sd8hhjfq

Any help was appreciated.

Best, Adam O

+4
source share
3 answers

thanks for the solution. I ended up working with the Sequel library. Essentially, I created the initializer, the slavedb_connect class, and use this connection to interact with the slave db if necessary.

I went through many attempts and very close. However, Sequel is an elegant solution that more closely matches the Rails / Ruby Way.

Thanks for the help / support. Really enjoy using this site.

0
source
+4
source

I would add a method to your model:

class Model def self.on_slave .. connect to slave with establish_connection .. yield .. connect to production with establish_connection end 

For each method that requires a slave, you can:

 def self.this_operates_on_the_slave on_slave do .. operate end end 
+1
source

All Articles