我有一个验证,看起来像这样:
class Book < ActiveRecord::Base belongs_to :author validates :name,uniqueness: { scope: :author_id } end
解决方法
是的,有一个
Proc
和:除非在验证器上.
class Book < ActiveRecord::Base belongs_to :author validates :name,uniqueness: { scope: :author_id },unless: Proc.new { |b| b.author_id.blank? } end
推荐阅读:http://guides.rubyonrails.org/active_record_validations.html#using-a-proc-with-if-and-unless