Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7545707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:59:57+00:00 2026-05-30T08:59:57+00:00

My Rails 3.1 application connects to 2 databases, one is the default, the other

  • 0

My Rails 3.1 application connects to 2 databases, one is the default, the other is an Amazon RDS MYSQL instance.

The current database.yml contains two production database connections. The models that need to pull from the second database simply use

establish_connection "production_on_amazon"

Unfortunately Heroku overwrites your database.yml, and only seems to inlcude one database connection. Does anyone know how I can add or configure my second?

Running “heroku config” I can see there are 2 DB’s listed but cant seem to configure to connect to both. Perhaps somehow set my default to the SHARED_DATABASE_URL db on Heroku and set the alternate to the DATABASE_URL which points to Amazon…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T08:59:58+00:00Added an answer on May 30, 2026 at 8:59 am

    Regarding Neil’s answer, here is a way to do it. Not an out-of-box solution, but might give you an idea…
    /lib/active_record_extensions.rb

    module ActiveRecordExtensions
      class Shard < ActiveRecord::Base
        #need to switch to the shard database connection from heroku config 
        primary_database_url = ENV['PRIMARY_DATABASE_URL']
    
        if(!primary_database_url.nil?)
          parsed_connection_string = primary_database_url.split("://")
          adapter = parsed_connection_string[0]
          parsed_connection_string = parsed_connection_string[1].split(":")
          username = parsed_connection_string[0]
          parsed_connection_string = parsed_connection_string[1].split("@")
          password = parsed_connection_string[0]
          parsed_connection_string = parsed_connection_string[1].split("/")  
          host = parsed_connection_string[0]
          database = parsed_connection_string[1]
    
          establish_connection(
            :adapter  => adapter,
            :host     => host,
            :username => username,
            :password => password,
            :database => database,
            :port     => 3306,
            :pool     => 5,
            :timeout  => 5000
          )
        else
          self.establish_connection "shard_#{Rails.env}"
        end
      end
    
      class ShardMigration < ActiveRecord::Migration
        def connection
          ActiveRecord::Shard.connection 
        end
      end
    end
    

    So your model should just extend ActiveRecord::Shard instead of Base

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am building a Rails application. The application uses MySql as the main database
I have rails application that connects to multiple databases. I wrote custom rake task
I have a Rails 3 application connected to a MySQL-database. The encoding used is
I have a rails application where each user has a separate database. (taking Joel
I am attempting to remotely connect to a MySQL database using ruby on rails.
When try to connect to the mysql server through my rails application, I get
in my current rails application I have a bunch of named routes defined to
I have a 5k line MySQL database structure from a legacy application and need
I have a Model (MessageImporter) that connects to a different Database than the other
I'm deploying my rails application on a shared host, using mysql. During development, however,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.