2016-09-30 62 views
-1

當我在我的模型中使用下面的一段代碼時,它可以很好地創建一個新的條目,但是當我試圖編輯表單時,我得到了相同的錯誤消息,這是顯示創建一個新的。實際的問題是,當我編輯相同的數據相同的表單條目,它不應該給這個錯誤。只有在創建具有以下驗證字段的重複數據時,它纔會給出錯誤消息。驗證在導軌2中編輯表單時發生錯誤?

module StudentModel 
    validate :is_valid_true 
private 
def is_valid_true 
    @batch = Batch.find(self.batch_id) 
    @batch.students.each do |s| 
    if ((s.full_name==self.full_name)&&(s.phone2==self.phone2)) 
     errors.add_to_base("Student with same Name and Mobile number is already Present in the Batch") 
     break 
    end 
    end 
end 

end 

回答