【问题标题】:Mongoid 7.0 single table inheritance with has_many带有 has_many 的 Mongoid 7.0 单表继承
【发布时间】:2020-08-17 19:03:59
【问题描述】:

我有一个 A 类

class A
    include Mongoid::Document
    has_many :bs
    accepts_nested_attributes_for :bs

还有一个B类

class B
    include Mongoid::Document
    belongs_to :a

还有一个继承自 B 的 C 类

class C < B
field :new_field, type: String

这适用于 Mongoid 6。使用 Mongoid 7,在带有 fields_for 的表单上,提交后,我现在得到:

Attempted to set a value for 'new_field' which is not allowed on the model B

注意,这不是 7.0(我相信)b/c 中支持的 mongoid 多态性,它不是在谈论单表继承(STI),而是它支持属于单个类/表的多个表作为同一个符号.这不是那个。而且我尝试过使用 as 和 polymorphic:true。

任何想法如何解决这个问题?

谢谢, 凯文

【问题讨论】:

    标签: mongoid sti


    【解决方案1】:

    让您使用 Mongoid 7.0 或 Rails 5.2(不确定是哪个更改破坏了这一点)您必须在 STI 表单中设置类型,我通过隐藏字段完成了此操作:

    <%f.hidden_field :_type, value: "C"%>
    

    这允许您设置仅后代属性。

    【讨论】:

      【解决方案2】:

      我遇到了类似的问题。我在一个项目中将 mongoid 从 3.0 升级到 7.0,并在旧应用程序中实现了 STI。 _type 在导致意外行为的父表中显式声明。我删除了它,项目开始工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-08
        相关资源
        最近更新 更多