【发布时间】:2015-06-09 08:30:20
【问题描述】:
我正在阅读文档,他们说关于 belongs_to:
指定与另一个类的一对一关联。这种方法 仅当此类包含外键时才应使用。
我有 3 个模型:
Info
Customer
Seller
我需要 Customer 或 Seller 的每个实例都可以链接到 infos 表中的一行。
所以我想我只需要将foreing_key info_id 放在2 个模型中,并指定一个belongs_to :info 关联。
但是当我这样做时:
@customer.info
它给了我错误,因为正在尝试执行此查询:
SELECT `auth_infos`.* FROM `auth_infos` WHERE `auth_infos`.`customer_id` = 1 LIMIT 1
但是对于我需要的以及我从文档中理解的内容,它应该执行这个:
SELECT `auth_infos`.* FROM `auth_infos` WHERE `auth_infos`.`id` = 1 LIMIT 1
我错过了什么?
【问题讨论】:
-
然后使用需要使用Polymorhic,关联
标签: ruby-on-rails ruby ruby-on-rails-4 associations