[2.3.5] How can I tell a model to use a specific DB and table?

So I have these two databases:

- Website - Company Intranet

I've built the DB and schema for the company intranet application to allow certain users (authenticated by AD within my rails app) to input data that will then be brought over to the first database (company public-facing website) when a rake task is run. That's the idea, anyway.

There is going to be a LOT of overlap between the two databases and their schemas. For example, both databases have a table named "jobs". So I'm trying to use multiple ActiveRecord connections to move data from the company intranet database to the website database without resorting to raw SQL.

So I have a Job class on the company intranet app: class Job < ActiveRecord::Base   # ... end

And one for the company website - it's in the company intranet application, but it's a different model: class WebsiteJob < ActiveRecord::Base   ActiveRecord::Base.establish_connection("website_#{RAILS_ENV}")   set_table_name "jobs"   # ... end

As you might be able to see, I'm running into a bit of a collision here. If I fire up script/console and call:

Job.first

I wind up with a Job from the right database (the "company intranet" database). However, if I call:

CompanyJob.delete_all # purge stale crap that nobody needs Job.first

It queries the FIRST database, the one that the CompanyJob model connects to. It seems to be switching the default connection behind the scenes and I'm not sure how to override that.

I tried manually assigning the ActiveRecord connection for the right database to the Job model, but it still exhibits this exact same behavior.

Does anyone know how I can force Rails to use the remote/other DB ONLY for that specific model, regardless of the fact that both databases contain identically named tables (that I'm unable to change)?

Thanks.

So I have these two databases:

  • Website

  • Company Intranet

I’ve built the DB and schema for the company intranet application to

allow certain users (authenticated by AD within my rails app) to input

data that will then be brought over to the first database (company

public-facing website) when a rake task is run. That’s the idea,

anyway.

There is going to be a LOT of overlap between the two databases and

their schemas. For example, both databases have a table named

“jobs”. So I’m trying to use multiple ActiveRecord connections to

move data from the company intranet database to the website database

without resorting to raw SQL.

So I have a Job class on the company intranet app:

class Job < ActiveRecord::Base

end

And one for the company website - it’s in the company intranet

application, but it’s a different model:

class WebsiteJob < ActiveRecord::Base

ActiveRecord::Base.establish_connection(“website_#{RAILS_ENV}”)

set_table_name “jobs”

end

As you might be able to see, I’m running into a bit of a collision

here. If I fire up script/console and call:

Job.first

I wind up with a Job from the right database (the “company intranet”

database). However, if I call:

CompanyJob.delete_all # purge stale crap that nobody needs

Job.first

It queries the FIRST database, the one that the CompanyJob model

connects to. It seems to be switching the default connection behind

the scenes and I’m not sure how to override that.

I tried manually assigning the ActiveRecord connection for the right

database to the Job model, but it still exhibits this exact same

behavior.

Does anyone know how I can force Rails to use the remote/other DB ONLY

for that specific model, regardless of the fact that both databases

contain identically named tables (that I’m unable to change)?

Might this be a symptom of poor design?

Share with us the tables for both databases.

Hey Charles, thanks for the feedback.

In the company intranet database we have:

It queries the FIRST database, the one that the CompanyJob model connects to. It seems to be switching the default connection behind the scenes and I'm not sure how to override that.

It's switching the default connection because you're asking it to - you're calling establish_connection on ActiveRecord::Base. You should just be calling it on the class you want to change.

Fred

Fred, as always, you are the man. Thanks for idiot checking me!

For any searchers/googlers with the same issue:

# THIS IS WRONG class Something < ActiveRecord::Base

ActiveRecord::Base.establish_connection("name_of_your_database_dot_yml_connection")   # ... end

# This appears right class Something < ActiveRecord::Base   self.establish_connection("name_of_connection_in_database_dot_yml")   # ... end