Search code examples
rubyvalidationsequel

Sequel ruby validates_unique case insensitive


I tried to validates uniqueness of couple (:name and :type) with Sequel ORM:

def validate
    super    
    validates_unique [:name, :type]
end

But Sequel is default to case sensitive. How can I validate this with case insensitive (not care about upcase or downcase)? Like a similar way of ActiveRecord:

validates_uniqueness_of :name, :case_sensitive => false


Solution

  • Not great, but from the docs:

       # If you want to do a case insensitive uniqueness validation on a database that
       # is case sensitive by default, you can use:
    
       validates_unique :column, :where=>(proc do |ds, obj, cols|
         ds.where(cols.map do |c|
           v = obj.send(c)
           v = v.downcase if v
           [Sequel.function(:lower, c), v]
         end)
       end)