oreoshake
oreoshake

Reputation: 4898

has_and_belongs_to external database join

This may be a case where I need to use has_many :through, but I'd prefer not to as this is a simple join table without attributes case.

My provider table is in an external database:

class Provider < ActiveRecord::Base
  establish_connection :external_db
  set_table_name :ch_cdn
  set_primary_key :cdn_id

  has_and_belongs_to_many :dashboards
end

Class Dashboard < ActiveRecord::Base
  has_and_belongs_to_many :providers
end

But when I try: Dashboard.first.providers << Provider.first

I get

ActiveRecord::StatementInvalid: Mysql2::Error: Table 'chshared.dashboards_providers' doesn't exist: SELECT ch_cdn.* FROM ch_cdn INNER JOIN dashboards_providers ON ch_cdn.cdn_id = dashboards_providers.provider_id WHERE dashboards_providers.dashboard_id = 1

It's looking in the wrong database for the join table. Is it possible to specify that 'dashboards' providers lives in the other database?

Upvotes: 0

Views: 278

Answers (1)

Jason
Jason

Reputation: 11

This might be a typo, but look at "Class Dashboard", should be "class Dashboard". Plus this is back from Nov. so I think you already solved it.

Upvotes: 1

Related Questions