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 6848849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:55:08+00:00 2026-05-27T00:55:08+00:00

My environment: Ruby 1.9.2p290, Rails 3.0.9 and RubyGem 1.8.8 unfortunately I have an issue

  • 0

My environment: Ruby 1.9.2p290, Rails 3.0.9 and RubyGem 1.8.8

unfortunately I have an issue when come across multiple database.

The situation is this: I have two model connect with two different database and also establishing association between each other.
database connection specifying in each model, look likes

class Visit < ActiveRecord::Base
  self.establish_connection "lab"
  belongs_to :patient
end

class Patient < ActiveRecord::Base
  self.establish_connection "main"
  has_many :visits
end

I got an error when meet following scenario

@visits = Visit.joins(:patient)

Errors: Mysql2::Error: Table ‘lab.patients’ doesn’t exist: SELECT visits.* FROM visits INNER JOIN patients ON patients.id IS NULL

Here ‘patients’ table is in ‘main’ database and ‘visits’ table in ‘lab’ database
I doubt when executing the code, that Rails is considering ‘patients’ table is part of ‘lab’ database [which holds ‘visits’ table].

  • 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-27T00:55:08+00:00Added an answer on May 27, 2026 at 12:55 am

    Well, I don’t know if this is the most elegant solution, but I did get this to work by defining self.table_name_prefix to explicitly return the database name.

    class Visit < ActiveRecord::Base
      def self.table_name_prefix
        renv = ENV['RAILS_ENV'] || ENV['RACK_ENV']
        (renv.empty? ? "lab." : "lab_#{renv}.")
      end
    
      self.establish_connection "lab"
      belongs_to :patient
    end
    
    class Patient < ActiveRecord::Base
      def self.table_name_prefix
        renv = ENV['RAILS_ENV'] || ENV['RACK_ENV']
        (renv.empty? ? "main." : "main_#{renv}.")
      end
    
      self.establish_connection "main"
      has_many :visits
    end
    

    I’m still working through all the details when it comes to specifying the join conditions, but I hope this helps.

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

Sidebar

Related Questions

we have a mixed development environment using ASP.NET MVC and Ruby on Rails. We
I have a mystery: My rvm/ruby-1.9.2 environment, running rails 3.1.3, can no longer run
Environment: Rails 3.0.4 and Ruby 1.9.2 I have the following form: <%= form_tag( {:action
I want to learn Ruby on Rails and have set up a test environment.
Somehow my rails installation hides itself successfully... I have: ruby 1.9.2p290 (2011-07-09 revision 32553)
I have a Ruby/Rails devvelopment environment using rbenv, MRI Ruby (1.9.2-p290), rails 3.0.9, and
My development environment: Ubuntu 9 Ruby 1.9.1/1.8.7 (rvm) Rails 2.3.5 Mysql 5.0 Apache Passenger
I am trying to set my DOS environment variable in Ruby, and have it
I see this all the time in Ruby: require File.dirname(__FILE__) + /../../config/environment What does
I have an app based on Rails 3 (version Rails 3.1.2 ) and ruby

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.