【发布时间】:2018-10-12 20:37:48
【问题描述】:
我有三个模型:
User(name: string)
Course(name: string)
Assoc(user_id: integer, ta_id: integer, teach_id: integer, student_id: integer)
我希望能够将用户与课程相关联,如助教、教师或学生。我不确定这是否符合 ActiveRecord 中的 :polymorphic 想法。这是我到目前为止所拥有的,但它并不完全有效:
class User < ApplicationRecord
has_many :tas, class_name: "Assoc", foreign_key: :ta_id
has_many :teaches, class_name: "Assoc", foreign_key: :teach_id
has_many :takes, class_name: "Assoc", foreign_key: :student_id
has_many :ta_courses, through: :tas
has_many :taken_courses, through: :tas
has_many :taught_courses, through: :tas
end
它不工作:
irb(main):056:0> User.find(1).ta_courses
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
Traceback (most recent call last):
1: from (irb):56
ActiveRecord::HasManyThroughSourceAssociationNotFoundError (Could not find the source association(s) "ta_course" or :ta_courses in model Assoc. Try 'has_many :ta_courses, :through => :tas, :source => <name>'. Is it one of ?)
irb(main):057:0>
任何指针将不胜感激!
【问题讨论】:
-
我会考虑将所有内容捆绑到单个连接模型/表中是否真的是一个好主意。你可以用几个离散的模型/表来完成同样的事情,避免创建一个上帝类——更重要的是有更好的索引。而且,您所做的不是多态性 - ActiveRecord 中的多态性是您使用 id 和 type 列动态连接表的地方。
-
你心目中的神级是哪一个?用户?我看到的两个替代方案似乎都更复杂:在用户周围具有实际的多态性,三种用户。或者,拥有三个独立的连接表。两者都更容易实现,确实如此,但似乎不必要地复杂。
-
Assoc 是神级,因为它将所有东西连接在一起并且没有单一的职责。
-
单表继承并不是真正的多态。它只是使用相同的机制——类型列。多态性是您创建指向多个表的单个关联的地方。多态性没有实际的外键,而是在 Ruby 中动态解析表之间的链接。
-
@max 也许... 通常我将术语“神级”与具有很多功能的模型联系起来。这个几乎没有。但也许吧。
标签: ruby-on-rails database activerecord