我有以下两个具有一对多关系的对象:
class DonationTarget < ApplicationRecord
has_many :purposes, inverse_of: :donation_target, autosave: true, dependent: :destroy
accepts_nested_attributes_for :purposes, allow_destroy: true
validates_associated :purposes
validates :purposes, presence: true, length: { maximum: 3 }
...
end
class Purpose < ApplicationRecord
belongs_to :donation_target, inverse_of: :purposes
with_options if: -> { ...(some method call on the donation_target)... } do |sth|
sth.validates : ....
end
end
以及用于捐赠目标的有效管理员编辑表单,包括目的-我们可以编辑,删除和创建捐赠目标的目的
ActiveAdmin.register DonationTarget do
...
form do |f|
...
f.inputs 'Purposes' do
f.has_many :purposes, heading: false, do |ff|
...
end
end
...
end
...
end
当我尝试加载编辑表单时,会触发目的类中的验证检查(with_options ...)。这会导致异常等等,但是我的问题是,为什么加载表单时也会触发验证?不应该仅在保存表单时触发它吗?