我想设置一个的has_many:穿过另外两个型号 用户和CustomerAccount之间的关系加入模型AccountOwnership (用户和account_ownerships表是在一个分贝,说DB1和customer_accounts表位于远程数据库中,比如说db2)。Rails的:从不同的数据库中的表之间建立has_many_through协会
下面是相关的代码,即树立协会
class User < ActiveRecord::Base
has_many :account_ownerships, :dependent => :destroy
has_many :companies, :through => :account_ownerships
end
class AccountOwnership < ActiveRecord::Base
belongs_to :user
belongs_to :company, :class_name => "Reporting::CustomerAccount"
end
class CustomerAccount < Reporting::Base
set_table_name 'customers'
establish_connection("db2_#{RAILS_ENV}")
end
的config/database.yml的(配置是正确的,虽然这里没有显示)
development:
reconnect: false
database: db1
pool: 5
db2_development:
reconnect: false
database: db2
host: different.host
pool: 5
在脚本/控制台
a = AccountOwnership.new(:user_id => 2, :company_id => 10)
a.user ## Returns the correct user
a.company ## returns the correct CustomerAccount instance
也
a.user.account_ownership ## returns a as anticipated
但
a.user.companies ## produces the following error:
#ActiveRecord::StatementInvalid: Mysql::Error: Table #'db2.account_ownerships' doesn't exist: SELECT `customers`.* FROM #`customers` INNER JOIN `account_ownerships` ON `customers`.id = #`account_ownerships`.company_id WHERE ((`account_ownerships`.user_id = 4))
在这里的“account_ownerships”和“用户”表中包含的 一个默认的数据库(比如DB1)的问题,“客户”表包含在 不同的数据库(比如说db2)。到数据库的连接是 配置正确,但在查找期间,因为只有一个数据库 可用的连接对象,Rails尝试在db2中查找account_ownerships 数据库,因此失败。
它看起来像我的设计/逻辑可能是有缺陷的,因为我看不到的方式 连接到使用同一个数据库连接两个不同的数据库,但我 会很激动,看是否有解决方法,在不改变 设计。 (我不愿意更改设计,因为db2不在我的 控件下)
看起来我可以通过将我的account_ownerships表移动到db2来解决此问题,但这对于我来说并不理想。
是否有任何其他机制/模式来设置此关联在 Rails中。
在此先感谢。 中号