2012-09-27 64 views
-1

我想禁止在我的接口表中有5列的非唯一組合。 例如以下欄目:只允許非唯一組合

:case_number, :client_name, :matter_name, :country, :engagement_code, 
these rows should be allowed: 
1,1,1,1,1 
1,1,1,1,1 
1,1,1,1,1 

and these rows should not be allowed: 
1,2,1,1,1 
2,3,1,1,1 
1,1,4,5,1 
+0

你確信你沒有這個逆轉嗎?我認爲應該允許底部的例子,頂部不允許。 –

回答

0

我想通了:

在模型中加入:

validate :non_unique_combination? 

def non_unique_combination?          
    if EvidenceImportInterface.count > 0 and 
     !(EvidenceImportInterface.exists?(:case_number => self.case_number, 
             :client_name => self.client_name, 
             :matter_name => self.matter_name, 
             :country => self.country, 
             :engagement_code => self.engagement_code))             
     errors.add(:case_number,'Combination not valid') 
    end 
end 
+0

它不是相反的。我想禁止非獨特的組合。我剛剛回答了我的問題。 – user1570144

+0

這是一個真正的問題。只需要仔細閱讀。我打算在從Excel導入數據時禁用非唯一組合。我計算出來併發布瞭解決方案。 – user1570144