-1

When i used the following piece of code in my model it worked fine for creating a new entry but when i tried to edit the form i am getting the same error message, which is showing for creating a new one. Actual problem is that when i edit the same form entry for the same data it shouldn't to give that error. It should give the error message only when duplicate data is created with following validated fields.

    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

1 Answers1

0

I'd recommend using validates_uniqueness_of with a scope. I think this is a more appropriate way to achieve what you want to accomplish. See: http://apidock.com/rails/v2.3.8/ActiveRecord/Validations/ClassMethods/validates_uniqueness_of.

nvugteveen
  • 496
  • 3
  • 6