public
Last active

Example Of Other/Legacy DB Connection Management & Query Cache

  • Download Gist
gistfile1.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
 
# 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 { yeild }
end
 
end

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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.