簡體   English   中英

Rails STI父模型has_many期望相關類的類型嗎?

[英]Rails STI Parent Model has_many expecting type on associated class?

給定STI父模型

class CustomForm < ApplicationRecord
has_many :templates` 
end

有幾個應該具有許多模板的子類。 為什么下面的模板類:

class FormTemplate < ApplicationRecord
belongs_to :custom_form

Rails為什么希望FormTemplate定義一個custom_form_type列?

您只需要custom_form_id。

這在以前甚至還沒有通過我的CI之前都是有效的,但是我做了其他一些工作似乎已經破壞了它:失望:

這是錯誤:

PG::UndefinedColumn: ERROR:  column form_templates.custom_form_type does not exist
LINE 1: ...CT “form_templates”.* FROM “form_templates”

因為您沒有發布所有代碼,所以代碼行導致了此錯誤。 我將根據您發布的內容進行猜測。

我認為您已將has_many :templates, as: :custom_form添加has_many :templates, as: :custom_form到CustomForm的子類之一。 它定義了[多態關聯]

在CustomForm模型has_many :templates` ,對我來說這沒有意義,除非您有一個模板模型,它是FormTemplate的子類。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM