Rails STI父模型has_many期望相关类的类型吗?

时间:2019-01-11 16:38:05

标签: ruby-on-rails ruby activerecord single-table-inheritance

给出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”

1 个答案:

答案 0 :(得分:0)

因为您没有发布所有代码,而代码行导致了此错误。我将根据您发布的内容进行猜测。

我认为您已将 has_many :templates, as: :custom_form添加到CustomForm的一个子类中。它定义了[多态关联]

在CustomForm模型has_many :templates`中,这对我来说没有任何意义,除非您拥有作为FormTemplate子类的模板模型

相关问题