有嵌套形式,关系是这样的
class Inspection < ActiveRecord::Base
has_many :inspection_components
accepts_nested_attributes_for :inspection_components
class InspectionComponent < ActiveRecord::Base
belongs_to :inspection
我在 Inspection 中有一个自定义验证方法,它取决于为 InspectionComponent 输入的属性。如何验证 - InspectionComponent 属性未保存或在检查验证中可用。
谢谢!
编辑:为了让事情更清楚一点,这是我正在尝试做的一个例子。
检查具有属性状态。InspectionComponent 也有一个属性 status。
检查编辑表单具有嵌套的检查组件,可以在此表单上更新每个模型的状态。如果所有@inspection_component.status == '完成',@inspection.status 应该只能被标记为'完成'。
因此,在验证@inspection 时,我必须能够看到用户为@inspection_component.status 输入的内容。
显然,我可以访问控制器中两个实例的参数,但是在应该进行验证的模型中,我看不到实现这一点的方法。
希望这很清楚,谢谢。