如何点foreign_key,而不是table_name_id的为类似的product_id在轨一belong_to故事3

问题描述:

我有3个表 集合,贡献者和ProductContributors如何点foreign_key,而不是table_name_id的为类似的product_id在轨一belong_to故事3

协会他们是如下 收藏 的has_many:product_contributors 的has_many:贡献者,:通过=> Product_contributors

贡献者 的has_many:product_contributors 的has_many:集合,:通过=> Product_contributors

ProductContributor belongs_to的:收集 belongs_to的:贡献者

我product_contributor表有作为 ID 的product_id(这里应该映射到collection_id如果产品类型是集合) PRODUCT_TYPE contributor_id

列有什么办法可以做到这一点?

我是想这样的事情 belongs_to的:采集,:foreign_key =>:PRODUCT_ID

PLS建议我笏可能是这个合适的解决方案...

您可以通过使用polymorphic associations实现这一目标

在product_contributor你shhould有:`belongs_to的:产品,多态=>真 和收集:的has_many:product_contributors,:如=>:产品

+0

thanx很多人... !! – 2012-08-11 09:31:03

+0

我编辑页面时仍然有问题。它在'on子句'中说:“Mysql2 ::错误:未知列'product_contributors.track_id':SELECT'tracks'。* FROM'tracks' INNER JOIN'product_contributors' ON'tracks'.'''''product_contributors'.' track_id' WHERE'product_contributors'.'contributor_id' ='026hboqqTQ'“ – 2012-08-22 13:02:14

+0

它仍然在寻找track_id而不是我有一个product_id为什么它...为什么它不采取product_id作为track_id或collection_id ... – 2012-08-22 13:05:26