모델 B에 대해 중첩 된 속성을 허용하는 모델 A가 있습니다. 모델 B 항목의 수와 항목이 필요한지 여부는 db의 세 번째 테이블을 기반으로 결정됩니다. 모델 B 항목에 대해 fields_for가있는 양식을 생성하려면 form_for를 사용합니다. 이것은 검증이 작동해야하는 방법이다 :rails : 중첩 된 형식의 유효성 검증 vs reject_if
- 모델 B에 대한 항목이 해당
:required
필드가 false 인 경우 공백이 허용되지만,이 저장되지해야 즉, 검증 오류가 발생하지 않아야이 항목 만:reject_if
에 의해 거부되어야합니다. :required
필드가 true 인 항목은 비워 둘 수 없습니다. 이러한 항목은:reject_if
에 의해 거부되어서는 안되지만 db에 저장 될 때 유효성 검사 오류가 발생합니다.class modelA < ApplicationRecord has_many :modelBs, dependent: :destroy accepts_nested_attributes_for :modelBs, reject_if: :not_required_and_blank? def not_required_and_blank? # return true if modelB.entry is blank && modelB.required_entry? is false end end class modelB < ApplicationRecord belongs_to :modelA validates :modelB_entry, presence: true, if: :required_entry? def required_entry? ModelC.find(:id).required end end
나는 (MODELA 클래스) 및 (modelB 클래스) validates_presence_of modelB_entry
방법 또는 :reject_if
또는 에서
accepts_nested_attributes_for
의 :reject_if
옵션이
- 해야
:validates_presence_of
에있는 모든 내용은?
먼저 실행되는 항목 : reject_if 또는 vaildation?