Search code examples
ruby-on-railsruby-on-rails-3validationvalidates-uniqueness-of

skip validation_uniqueness for certain values


I have validation on uniqueness and I want skipping certain value or values(for example 0000):

validates_uniqueness_of :gtin, :scope => [:user_id, :item_id]

I'm tried to use next construction, but she don't work:

validates_uniqueness_of :gtin, :scope => [:user_id, :item_id], :unless => Proc.new{|base_item| base_item.gtin == '0000'}

How I can skip certain value or values? Thanks.

P.S. update!!! did not see a manual migration, which change behaviour


Solution

  • using the :unless option is certainly the right way, but i think you get the whole object as proc argument so it should be

    validates_uniqueness_of :gtin, :scope => [:user_id, :item_id], :unless => Proc.new{|obj| obj.gtin == '0000'}