仅允许非唯一组合

时间:2012-09-27 17:18:48

标签: ruby-on-rails ruby ruby-on-rails-3.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

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