1

There are many questions and answers regarding the creation of multiple connections to multiple databases in rails:

https://stackoverflow.com/a/7480330/2120023

https://stackoverflow.com/a/6305540/2120023

Example from outside stackoverflow: http://ilikestuffblog.com/2012/09/21/establishing-a-connection-to-a-non-default-database-in-rails-3-2-2/

But I have yet to find a solution that works when using a model that appears in both databases.

If my default db has a table titles and my Other db has a table titles how do I access the other database's Title model?

title.rb:

class Title < ActiveRecord::Base
end

othertitle.rb:

class Other < ActiveRecord::Base
  self.abstract_class = true
  establish_connection "other_#{Rails.env}"
end

class OtherTitle < Other
end

I can't use the above because I get this error (EDIT: for clarity there is no other_titles table in either db only a titles table -> EDIT 2: If I do create an other_titles table in the Other database, everything works perfectly but that does not help me access the titles table.):

ActiveRecord::StatementInvalid: Mysql2::Error: Table 'other.other_titles' doesn't exist: SHOW FULL FIELDS FROM `other_titles`

I also can't use class Title < Other because I get a TypeError: superclass mismatch for class Title error.

database.yml

development:
  adapter: mysql2
  encoding: utf8
  database: db_dev
  pool: 5
  username: xxxx
  password: xxxx
  socket: /var/lib/mysql/mysql.sock

production:
  adapter: mysql2
  encoding: utf8
  database: db
  pool: 5
  username: xxxx
  password: xxxx
  socket: /var/lib/mysql/mysql.sock


other_development:
  adapter: mysql2
  encoding: utf8
  database: other_dev
  pool: 5
  username: xxxx
  password: xxxx
  socket: /var/lib/mysql/mysql.sock

other_production:
  adapter: mysql2
  encoding: utf8
  database: other
  pool: 5
  username: xxxx
  password: xxxx
  socket: /var/lib/mysql/mysql.sock 
Community
  • 1
  • 1
paulguy
  • 1,045
  • 16
  • 28
  • did you create appropriate database connection configs on you database.yml file for nasa_#{dev} ? – Said Kaldybaev Feb 05 '14 at 20:55
  • Yes, but the example technically should have `other_` and not `nasa_` I'll edit it to show correct code. – paulguy Feb 05 '14 at 20:57
  • post your database.yml file pls. – Said Kaldybaev Feb 05 '14 at 20:59
  • @Kaldybaev added `database.yml` as requested. – paulguy Feb 05 '14 at 21:04
  • as per your database file, you have four environments, each envt can use same or different database. what is creating the issue or may be i didn't get it properly. Have you checked with this http://apidock.com/rails/ActiveRecord/Base/establish_connection/class – Bijendra Feb 06 '14 at 12:53
  • @GhostRider I do not have any problem connecting to the databases I choose. If I create an `other_titles` table in the `Other` database, everything works perfectly. The issue is that I cannot access the `titles` table in the `Other` database. – paulguy Feb 06 '14 at 13:09

1 Answers1

1

I found the answer and it is quite simple:

class OtherTitle < Other
  self.table_name='titles'
end

Referenced here:

Efficient way to pull data from second database?

and here:

Cannot connect to two postgres databases in rails 3.2.

Community
  • 1
  • 1
paulguy
  • 1,045
  • 16
  • 28