세 가지 모델이 있습니다. 판매, 항목 및 이미지. 판매가 생성 될 때 판매 당 최소 3 장의 사진과 하나 이상의 품목이 있음을 확인하고 싶습니다. 이것을 달성하는 가장 좋은 방법은 무엇입니까?레일스 accepts_nested_attributes 카운트 유효성 확인
판매 모델 :
class Sale < ActiveRecord::Base
has_many :items, :dependent => :destroy
has_many :images, :through => :items
accepts_nested_attributes_for :items, :reject_if => lambda { |a| a[:title].blank? }, :allow_destroy => true
end
항목 모델 :
class Item < ActiveRecord::Base
belongs_to :sale, :dependent => :destroy
has_many :images, :dependent => :destroy
accepts_nested_attributes_for :images
end
이미지 모델 :
class Image < ActiveRecord::Base
belongs_to :item, :dependent => :destroy
end
이러한 메소드의 이름을 validate_item_count 및 validate_image_count로 지정하는 것이 이상적입니다. 이는 사용자의 의도를 명확히하고 메소드가 오류를 추가하기 때문입니다. – joelparkerhenderson
좋은 점, 추가해 주셔서 감사합니다. – digicazter