鉴于您具有可以关联任意类(文档)的 M:M 关系,我认为您会查看双面多态关联。
您可能有一个DocumentAssociation
类,例如:
# == Schema Information
#
# Table name: document_associations
#
# id :integer not null, primary key
# base_document_type :string
# base_document_id :integer
# associated_document_type :string
# associated_document_id :integer
# created_at :datetime not null
# updated_at :datetime not null
#
class DocumentAssociation < ApplicationRecord
belongs_to :base_document, polymorphic: true
belongs_to :associated_document, polymorphic: true
end
然后是这样的:
class Drawing < ApplicationRecord
has_many :base_document_associations, class_name: 'DocumentAssociation', as: :base_document
has_many :associated_documents, through: :base_document_associations
end
这可能在方向上是正确的,但是您可能需要做一些摆弄。如果您希望能够在两个方向上导航(即,对于给定的绘图,您需要绘图既是 base_document 又是 associated_document 的所有关联),您还必须做一些额外的提升。