Rails 提供validates_associated,这将确保相关记录有效。如果关联记录无效,则不保存父记录。
在Foo:
class Foo < ApplicationRecord
has_many :bars, dependent: :destroy
accepts_nested_attributes_for :bars, allow_destroy: true
validates_associated :bars
end
在Bar:
class Bar < ApplicationRecord
belongs_to :foo
include ActiveModel::Validations
validates_with BarValidator
end
在BarValidator,即a custom validator:
class BarValidator < ActiveModel::Validator
def validate(record)
record.errors.add :some_error, 'Some message' unless condition_met?
end
end
你说过:
只有当它的子模型符合一些验证,比如它们的属性总和等于来值等等?
这有点模棱两可。如果您确实确实需要计算子项的总和,那么您可以向父项添加一个验证器,该验证器通过子项进行映射并在未能满足条件时附加错误:
在Foo,(或者最好是验证者):
validate :children_condition
def children_condition
errors[:base] << "Some message" if bars.map(&:attribute).sum != expected_minimum_value
end
validates_related 文档中的重要说明:
警告:不得在关联的两端使用此验证。这样做会导致循环依赖,导致无限递归。
注意:如果尚未分配关联,则此验证不会失败。如果要确保关联既存在又保证有效,还需要使用 validates_presence_of。
至于:
另外,我如何使用 rspec 对其进行测试?
我将创建一个包含有效父、有效子 && 有效父、无效子等的测试套件,并期望 Model.count 增加了预期的数量(在后一个示例中为零)。