Validation to ensure uniqueness of but ignoring empty values?

前端 未结 1 1769
萌比男神i
萌比男神i 2021-01-01 10:56

I have the following model field validation:

validates_uniqueness_of :acronym, :scope => [:group_id], :case_sensitive => false

The pr

1条回答
  •  别那么骄傲
    2021-01-01 11:14

    Yes, there are two possible options that you can pass to validations for optional fields: :allow_blank or :allow_nil, which will skip the validations on blank and nil fields, respectively. If you change your validation to the following, you should get the behaviour you want:

    validates_uniqueness_of :acronym, :allow_blank => true, :scope => [:group_id], :case_sensitive => false
    

    0 讨论(0)
提交回复
热议问题