has_and_belongs_to 外部数据库连接

发布于 2024-12-18 14:48:32 字数 892 浏览 2 评论 0原文

这可能是我需要使用 has_many :through 的情况,但我不想这样做,因为这是一个没有属性情况的简单连接表。

我的提供者表位于外部数据库中:

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

但是当我尝试时: Dashboard.first.providers << Provider.first

我得到

ActiveRecord::StatementInvalid: Mysql2::Error: 表 “chshared.dashboards_providers”不存在:SELECT ch_cdn.* FROM ch_cdn 内连接 dashboards_providers ON ch_cdn.cdn_id = dashboards_providers.provider_id 地点 dashboards_providersdashboard_id = 1

它在错误的数据库中查找连接表。是否可以指定“仪表板”提供程序位于另一个数据库中?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤君无依 2024-12-25 14:48:32

这可能是一个错字,但是看看“Class Dashboard”,应该是“class Dashboard”。另外,这是从 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文