Ruby on rails 与嵌套表单字段的条件关联不';无法正确验证
轨道6.0.0.0.3 Ruby 2.6.1 我在Ruby on rails 与嵌套表单字段的条件关联不';无法正确验证,ruby-on-rails,validation,nested-forms,has-many,belongs-to,Ruby On Rails,Validation,Nested Forms,Has Many,Belongs To,轨道6.0.0.0.3 Ruby 2.6.1 我在项目和变量之间有两个模型关联,如下所示: class Item < ApplicationRecord ... has_many :variants accepts_nested_attributes_for :variants, reject_if: :all_blank, allow_destroy:
项目
和变量
之间有两个模型关联,如下所示:
class Item < ApplicationRecord
...
has_many :variants
accepts_nested_attributes_for :variants,
reject_if: :all_blank,
allow_destroy: true
end
class Variant < ApplicationRecord
belongs_to :item, -> { where(kind: :article) }
end
它在控制台上工作,但不适用于嵌套表单字段。
其他相关已知问题案例:我建议在
变体
模型中验证项目类型,而不是中所属的
因为在你的例子中,Item.create!(种类:新奇)。变体。创建
正在引发错误(项在我尝试时必须存在)
顺便说一句,我们感兴趣并做了一个最小的测试报告()(虽然没有这个表格)。我建议在变量模型中对项目类型进行验证,而不是在中的所属的中进行验证
因为在你的例子中,Item.create!(种类:新奇)。变体。创建
正在引发错误(项在我尝试时必须存在)
顺便说一句,我们感兴趣并做了一个最小的测试repo()(虽然没有这个表单)
item = Item.create(kind: :article)
item.variants.create
...
item = Item.create(kind: :novel)
item.variants.create
it raises validation error 'should be of kind: :article only'