0

我正在使用 Rails 3.2.8,我想确保表的 1 行中的一组属性的唯一性,但我可以有多个具有相同值的行。validates_uniqueness_of 验证器似乎不适用于此。为了给出一些上下文,这些是彩票号码选择。

4

1 回答 1

0

为确保winning_number_X!= winning_number_Y,请尝试:

# in your model

UNIQ_FIELDS = [:winning_number_1, :winning_number_2, :winning_number_3,
  :winning_number_4, :winning_number_5, :winning_number_6, :winning_number_bonus]

validate :numbers_uniquness

# ...    

def numbers_uniquness 
  unless UNIQ_FIELDS.map{|field| self[field] }.uniq.length == UNIQ_FIELDS.length
    errors[:base] << "Numbers have to be uniq" 
  end
end
于 2012-11-26T17:02:27.267 回答