我有以下自嵌套的父/子模型
class Vmt::EvaluationUnit < ActiveRecord::Base
has_many :children, class_name: "Vmt::EvaluationUnit", foreign_key: :evaluation_unit_id, dependent: :destroy
belongs_to :parent, class_name: "Vmt::EvaluationUnit", foreign_key: :evaluation_unit_id
accepts_nested_attributes_for :children, reject_if: :all_blank, :allow_destroy => true
validates :evaluation_name, presence: true, if: Proc.new { |e| e.parent.nil? }
end
但是当我尝试创建带有子项的新顶级单元时,test_name的验证规则不起作用。以下是包含一个子项的Create-Request的日志条目:
Started POST "/vmt/evaluation_units" for 192.168.0.117 at 2015-08-20 10:36:39 +0200
Processing by Vmt::EvaluationUnitsController#create as HTML
Parameters: {"utf8"=>"?", "authenticity_token"=>"xQjGeIr/I+iPi+KG+0SLIH06JzIbOd8synWtvb1+ksY=", "vmt_evaluation_unit"=>{"evaluation_name"=>"Neu", "name"=>"Gesamt", "max_point"=>"200", "children_attributes"=>{"1440059791662"=>{"name"=>"Neu1", "max_point"=>"200", "_destroy"=>"false"}}}, "commit"=>"Speichern"}
BEGIN
ROLLBACK
看来,在验证发生时,父母也没有为孩子设置。如何在记录没有父项时验证:evaluation_name是否存在?