3

I am trying out multiple databases in Rails 4.0 in an app i'm making. Based on this: Connecting Rails 3.1 with Multiple Databases and http://api.rubyonrails.org/classes/ActiveRecord/Base.html

I was able to create the some models that use different connections, however, when I try to inherit the connection from an abstract class to keep my code DRY, Rails 4.0 automatically assumes that I'm trying to do Single Table Inheritance when I'm querying info and then throws a Mysql::Error that the table doesn't exist.

Here are some code snippets to shed some light on my current state Base Class:

Class Mps < ActiveRecord::Base
  self.abstract_class = true
  establish_connection "mps"
end

Child Class:

Class TblAdmVsl < Mps
  self.table_name = "tbladmvsl"
end

Is there any way around this as I want to keep my code as DRY as possible. Below is the error I received when i tried this in the rails console

2.0.0-p247 :005 > TblAdmVsl.first
  TblAdmVsl Load (4.5ms)  SELECT `tbladmvsl`.* FROM `tbladmvsl` ORDER BY `tbladmvsl`.`id` ASC LIMIT 1
  Mysql::Error: Table 'mps2_tbl.mps' doesn't exist: SHOW FULL FIELDS FROM `mps`
  ActiveRecord::StatementInvalid: Mysql::Error: Table 'mps2_tbl.mps' doesn't exist: SHOW     FULL FIELDS FROM `mps`
        from /home/maru/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:286:in `query'
        from /home/maru/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:286:in `block in execute'
Community
  • 1
  • 1
maru
  • 81
  • 1
  • 8

1 Answers1

1

Try this:

Class Mps < ActiveRecord::Base
  self.abstract_class = true
  self.table_name = "mps"
  establish_connection self.table_name
end

class TblAdmVsl < Mps
  self.table_name = "tbladmvsl"
end

I'm not sure, that there is an functionality to establish connection only by db name, maybe you override this function somewhere, but the standard solution for this is to connect passing config(copy of database.yml) file:

new_config = ActiveRecord::Base.configurations[Rails.env].clone
new_config['database'] = "another_database_name"
ActiveRecord::Base.remove_connection #optional if you need to remove current connection
ActiveRecord::Base.establish_connection new_config
Andrey Yasinishyn
  • 1,851
  • 2
  • 23
  • 36
  • 2
    Mps isn't supposed to have a table, its just a placeholder for the connection to the different database that will be used by the tables inheriting from it. I wanted to bypass the Single Table Inheritance feature that comes by default and only inherit the connection. – maru Sep 18 '13 at 09:06
  • Function that executes the inheritance will not be called in Mps class?? If not, then simply delete selt.table_name.. And I sure that ActiveRecord::Base.establish_connection function argument must an config file.. You may take a clone of current one with this commant `new_config = ActiveRecord::Base.configurations[Rails.env].clone` – Andrey Yasinishyn Sep 18 '13 at 09:15
  • 1
    my problem is not that I can't access the connection, its that rails forces the Single Table Inheritance feature when I try to inherit a model for its connection attribute. Either rails 4 updated something that caused this to not work anymore, or I've botched up some configuration somewhere. – maru Sep 18 '13 at 09:33