1

I am trying to make a custom validator for uniqueness. The following example does not work for some reason:

validates :language_id, uniqueness: { scope: :country_id}, if: :no_detail?
  validates :language_id, uniqueness: { scope: [:country_id,:city_id, :province_id]}, if: :detail?

  def no_detail?
    if city_id.blank? && province_id.blank?
        return true
    else
        return false
    end
  end

  def detail?

    return !no_detail?
  end

The error I am getting is that there are duplicate language_ids when I create a new data entry.

Alex C
  • 1,334
  • 2
  • 18
  • 41
  • 1
    You should a have a record with language_id with the same value as the record you are trying to save, have you checked? – sites May 06 '13 at 19:52
  • 1
    which is your new data entry?, and what data entries you have? – sites May 06 '13 at 19:54
  • @juanpastas, there was only 1 entry, the new entry had the same language_id but a different country_id. – Alex C May 07 '13 at 20:44
  • That's what you want, isn't it? you are validating: allow (countryA,langA), (countryB, langA); deny (countryA, langA), (countryA, langA). You get this with first statement – sites May 07 '13 at 21:13

0 Answers0