0
我確信有一個更好的標題爲此,所以很抱歉。Rails:Single Record,Many Polymorphic
我有一個Verification
模型。理想情況下,會有各種其他模型類型能夠將自己與單個Verification
記錄相關聯。例如。
Verification(id: 1, verifiable_type: 'Reference')
Reference(id: 1, verification_id: 1)
Reference(id: 2, verification_id: 1)
Verification(id: 2, verifiable_type: 'Degree')
Degree(id: 1, verification_id: 2)
Degree(id: 2, verification_id: 2)
我希望的是在該has_many
動態:class_name
選項簡單的東西:
class Verification < ActiveRecord::Base
has_many :verifiables, class_name: -> { dynamic_class_name }
end
我99%肯定,反向協會將實際工作的開箱。因此,使用上面的記錄,以下應該是沒有問題的:
class Reference < ActiveRecord::Base
belongs_to :verification
end
class Degree < ActiveRecord::Base
belongs_to :verification
end
有什麼建議嗎?
你可以添加你的域邏輯的更多細節更換呢?爲什麼單個'Verification'實例需要引用多個驗證?您可能最終需要一個與驗證和驗證具有一對一關聯的中間模型 - 我認爲ActiveRecord不提供這種功能。 –
驗證對象充當開始驗證對象的佔位符。它可以是'1',可以是'4'。該應用程序有許多不同的東西可以驗證,所有驗證都將通過'/ verify /:type /:access_code'進行處理。 – bschaeffer