Skip to content

Instantly share code, notes, and snippets.

@dimanyc
Forked from metaskills/gist:4065702
Created September 2, 2016 22:17
Show Gist options
  • Save dimanyc/fd9568f4b58991c5b255a0d18adfd3d8 to your computer and use it in GitHub Desktop.
Save dimanyc/fd9568f4b58991c5b255a0d18adfd3d8 to your computer and use it in GitHub Desktop.
Example Of Other/Legacy DB Connection Management & Query Cache
# 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