3

I've faced a trouble while implementing multiple database connections in a Rails 4 application. Along with the primary database connection I've created secondary connection by specifying details in database.yml.

secondary_base:
 adapter: postgresql
 encoding: unicode
 host: localhost
 database: secondary_db
 pool: 5
 username: postgres
 password: postgres

Then created a model named SecondaryBase which holds the connection to that secondary database. Code given below:

secondary_base.rb

class SecondaryBase < ActiveRecord::Base
  self.abstract_class = true
  establish_connection "secondary_base"
end

Then added two models Member and Subdomain.

member.rb

class Member < SecondaryBase
  has_and_belongs_to_many :subdomains
end

subdomain.rb

class Subdomain < SecondaryBase
  has_and_belongs_to_many :members
end

Now as you can see that Member and Subdomain models are related by has_and_belongs_to_many relationship. So when I'm trying to insert data to the join table named members_subdomains, It's now working and giving me the error something like that PG: Undefined table, though the table exits in secondary_db database. What I understood that rails is trying to find the members_subdomains table in primary database.But when I tried the same code in Rails 3.2.13, then there were no problem at all, everything is fine. Have any of you deal with a similar problem before? Please help.

3
  • afaict, this appears to be a breaking change in Rails 4.1. I've just upgraded an application with this scenario and encountered the same problem. is there an issue on rails github that you know of? Commented Mar 21, 2016 at 11:33
  • @MikeCampbell I didn't found any helpful resources on github or other sites that time.
    – Avishek
    Commented Mar 21, 2016 at 16:43
  • ok, thanks for letting me know :) Commented Mar 21, 2016 at 16:50

1 Answer 1

3

I believe you will need to switch from has_and_belongs_to_many to has_many :through to get this to work. In order to do that, just create a model for the join table - I'll call it `MemberSubdomain'.

class SecondaryBase < ActiveRecord::Base
  self.abstract_class = true
  establish_connection "secondary_base"
end

class MemberSubdomain < SecondaryBase
  self.table_name = 'members_subdomains'
end

class Member < SecondaryBase
  has_many :member_subdomains
  has_many :subdomains, :through => :member_subdomains, :source => :subdomain
end

class Subdomain < SecondaryBase
  has_many :member_subdomains
  has_many :members, :through => :member_subdomains, :source => :member
end
2
  • Then what is the actually the problem with has_and_belongs_to_many?
    – Avishek
    Commented Feb 13, 2015 at 4:31
  • The problem is that it's not associated with a model so there is no way to tell it which database to connect to.
    – infused
    Commented Feb 13, 2015 at 4:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.