【发布时间】:2010-09-29 21:18:18
【问题描述】:
我正在尝试设置一个 has_many :through 两个模型之间的关系 User 和 CustomerAccount 通过另一个连接模型 AccountOwnership (users 和 account_ownerships 表在一个 db 中,比如 db1,customer_accounts 表在远程 db 中,比如 db2)。
这是建立关联的相关代码
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: 表
#'db2.account_ownerships' 不存在:SELECT `customers`.* FROM
#`customers` INNER JOIN `account_ownerships` ON `customers`.id =
#`account_ownerships`.company_id WHERE ((`account_ownerships`.user_id
= 4))
这里的问题是“account_ownerships”和“users”表包含在 一个默认数据库(比如 db1),“客户”表包含在 一个不同的数据库(比如 db2)。与数据库的连接是 配置正确,但在查找过程中,因为只有一个数据库 连接对象可用,Rails 尝试查找 account_ownerships db2 中的数据库,因此失败。
看起来我的设计/逻辑可能有缺陷,因为我看不到方法 使用相同的数据库连接连接到两个不同的数据库,但我 很高兴看到是否有解决方法,而无需更改 设计。 (我不愿意更改设计,因为 db2 不在我的 控制)
看起来我可以通过将 account_ownerships 表移动到 db2 来解决这个问题,但这至少对我来说不太理想。
是否有任何替代机制/模式来设置此关联 导轨。
提前致谢。 M
【问题讨论】:
标签: ruby-on-rails activerecord has-many-through model-associations