2017-03-10 46 views
1

我試圖驗證夫婦的獨特性(:名稱和:型)續集ORM:續集紅寶石validates_unique不區分大小寫

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

但續集默認爲區分大小寫。我怎樣才能驗證這與大小寫不敏感(不關心upcase或downcase)?比如ActiveRecord的類似的方式:

validates_uniqueness_of :name, :case_sensitive => false

回答

0

不是很大,但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) 
+0

謝謝你,我甚至可以驗證多個列: 'validates_unique [:類型,:名字]' – hiepph