0

In my model, I have:

validates_presence_of :start_date, :message => 'Please enter date.'
validate :start_date_exist_check , on: :create`

def start_date_exist_check 
  @check_date = Employee.where('start_date = ?', start_date)`
  if @check_date.blank?
  end
end

validate_presence_of is executed after my module start_date_exist_check, which creates a problem.

Can anybody tell me how I can validate the field first?

sawa
  • 165,429
  • 45
  • 277
  • 381
anjali
  • 87
  • 1
  • 12

1 Answers1

0

First check whether the start date is blank or not , if its blank, return it from first line

validates_presence_of :start_date, :message => 'Please enter date.'
validate :start_date_exist_check , on: :create`

def start_date_exist_check 
  return if start_date.blank?
  @check_date = Employee.where('start_date = ?', start_date)`
  if @check_date.blank?
  end
end

Not tested let me know if its not work

Vishal
  • 7,113
  • 6
  • 31
  • 61