简体   繁体   中英

ActiveRecord::StatementInvalid (PG::InvalidDatetimeFormat: ERROR: invalid input syntax for type date: “”

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM