Skip to content

Instantly share code, notes, and snippets.

@metaskills
Last active September 2, 2016 22:17
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save metaskills/4065702 to your computer and use it in GitHub Desktop.
Save metaskills/4065702 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
@brentmc79
Copy link

Not sure how gist pull requests work... https://gist.github.com/4449608

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment