의 존재를 검증 나는이 다음 모델HABTM 모델 관계
class Business < ActiveRecord::Base
has_and_belongs_to_many :categories
validates_presence_of :category_ids
end
class Category < ActiveRecord::Base
has_and_belongs_to_many :businesses
end
나는 category_ids
속성을 사용하여 비즈니스 창출 양식을 통해 관계를 설정하고있다.
validates_presence_of
을 사용해 보았지만 카테고리의 존재를 확인하지 못했습니다.
브라우저를 통해 양식을 조작 할 수 있으며 범주에 존재하지 않는 ID를 부여 할 수 있습니다. 양식을 제출 한 후 오류가 발생합니다 :
Couldn't find Category with id=181723
편집 :
추가 된 다음 사용자 정의 검증 방법, 검증이 실행되고 있지 것처럼하지만, 난 여전히 같은 오류를 얻고있다 .
class Business < ActiveRecord::Base
has_and_belongs_to_many :categories
validate :categories_exist
def categories_exist
category_ids.each do |c|
errors.add(:category_ids, :category_doesnt_exist) unless Category.exists? c
end
end
end
감사합니다. 사용자 지정 유효성 검사를 사용하여 최종 솔루션을 게시합니다. – AlexBrand
시도했지만 여전히 오류가 발생합니다 .. 편집을 참조하십시오. – AlexBrand