Search code examples
ruby-on-railsherokumultiple-databases

Heroku follow : how to handle multiple databases in a Rails app?


We have a Rails app hosted on Heroku and i need to set up some analytics pages for our marketing guy. This seems to be the typical use case for a Heroku follow database

The 2 databases will share the same model classes, i want only some action to query the replicated database.

What is the preferred way to handle this situation in my app ?

ActiveRecord::Base documentation talks about class specific connection :

You can also set a class-specific connection. For example, if Course is an ActiveRecord::Base, but resides in a different database, you can just say Course.establish_connection and Course and all of its subclasses will use this connection instead.

Should i subclass all my model classes to specify connection to a secondary database ? (will it even work ?)

I also found this gem but it seems not maintained anymore.... magic_multi_connections gem ?


Solution

  • To better answer this question : Heroku has now posted an "official" way of dealing with this situation using the Octopus gem :

    https://devcenter.heroku.com/articles/distributing-reads-to-followers-with-octopus

    With this gem it's possible to create either a fully replicated model, or execute only a block on the follower :

    Octopus.using(:slave_two) do
      User.create(:name => "Mike")
    end