29 Sep '13, 4am

Using ActiveRecord with multiple databases via @pivotallabs

At Pivotal I’ve been working on a project which uses two databases. Doing some quick searching we came up with a rather naive solution, this quick mixin: module SecondDatabaseMixin extend ActiveSupport::Concern included { establish_connection "db2_#{Rails.env}" } end It didn’t become obvious what was wrong with this until we added one too many models. Suddenly we started getting complaints from the database that we had too many connections open. Reading the documentation for ActiveRecord it became clear that the correct way to share connections is to use inheritance. This was the solution we came up with: class SecondDatabaseModel < ActiveRecord::Base self.abstract_class = true establish_connection "db2_#{Rails.env}" end Now any model that inherits from SecondDatabaseModel gets a connection to the correct database, and we only have as many connections as needed.

Full article: http://pivotallabs.com/using-activerecord-with-multiple-d...

Tweets

Pivotal Labs Tech Talks

pivotallabs.com 03 Oct '13, 7am

Patrick McFadin gives an introduction to Cassandra and explains how the database can excel in distributed contexts to prov...

Why we don’t use OOCSS (but we should) via @piv...

pivotallabs.com 27 Sep '13, 2pm

I recently had the opportunity to pair with Nicole Sullivan (of OOCSS fame) for a few days on my current project. I learne...

Software engineering team objective: minimize d...

pivotallabs.com 29 Sep '13, 3pm

Like most engineers, I do a lot of optimizing, often just for fun. When walking to work I seek the shortest route. When fo...