Last active
September 2, 2016 22:17
-
-
Save metaskills/4065702 to your computer and use it in GitHub Desktop.
Example Of Other/Legacy DB Connection Management & Query Cache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Assuming you champioin your other DB connection with a class. | |
module OtherDb | |
class Connection < ActiveRecord::Base | |
establish_connection :other_db | |
self.abstract_class = true | |
end | |
end | |
# Alway use the connection for other/legacy connections. | |
module OtherDb | |
class SomeModel < OtherDb::Connection | |
self.table_name = 'db_table_name' | |
self.primary_key = 'view_unique_identifier' | |
end | |
end | |
# Now let ActiveRecord cache those queries per request/response. | |
class ApplicationController < ActionController::Base | |
around_filter :cache_other_db_connection | |
protected | |
def cache_other_db_connection | |
OtherDb::Connection.connection.cache { yield } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Not sure how gist pull requests work... https://gist.github.com/4449608