Ruby on rails 跳过具有条件的子验证

Ruby on rails 跳过具有条件的子验证,ruby-on-rails,ruby,Ruby On Rails,Ruby,我有两个ActiveRecords作者和一本书 这里的最佳解决方案是什么?您需要为条件验证提供一个过程 validates :score_url, presence: true, if: Proc.new { |book| book.author.scientist? } 如果您的验证变得更复杂,您应该将逻辑提取到一个新方法中 validates :score_url, presence: true, if: :author_scientist? private def author_sc

我有两个ActiveRecords作者和一本书

这里的最佳解决方案是什么?

您需要为条件验证提供一个过程

validates :score_url, presence: true, if: Proc.new { |book| book.author.scientist? }
如果您的验证变得更复杂,您应该将逻辑提取到一个新方法中

validates :score_url, presence: true, if: :author_scientist?

private

def author_scientist?
  author.present? && author.scientist?
end
validates :score_url, presence: true, if: Proc.new { |book| book.author.scientist? }
validates :score_url, presence: true, if: :author_scientist?

private

def author_scientist?
  author.present? && author.scientist?
end