0
두 개의 ActiveRecords Author
및 Book
이 있습니다.조건으로 자녀 확인을 건너 뛰십시오.
class Author < ActiveRecord::Base
has_many :books
enum author_type: {
musician: 0,
scientist: 1
}
accepts_nested_attributes_for :books
end
class Book < ActiveRecord::Base
belongs_to :author
validates :name, presence: true
validates :score_url, presence: true
end
지금 Book
모두 name
및 score_url
, 에 대한 존재의 유효성을 검사하지만 author.author_type
이 scientist
때 score_url
에 대한 검증을 생략 할 수 있습니다.
이 방법을 시도했지만 생성 중에 author
을 찾을 수 없습니다.
class Book < ActiveRecord::Base
belongs_to :author
validates :name, presence: true
validates :score_url, presence: true, if: "author.scientist?"
end
가장 좋은 해결책은 무엇입니까? 유효성 검사가 더 복잡해 경우