mercredi 13 décembre 2017

validation with two diffrent objects but same class

So i have this model Appointment:

validates :purpose, :extra, :appointment_date, :appointment_time, presence: true

and now this is situation: i want to get error if someone will want to make appointment in the same day and the same time. So i have to compare two objects of the same class and i have no idea how i can do it.

only one thing comes into my mind

def allready_booked?
  @employee = Employee.find(params[:employee_id]) <----- this is the part i dont know how to do it
  @appointments = Appointment.where(appointment_date: appointment_date).where(employee_id: employee.id)
  @appoitnments.each do |appo|
    if(appo.appointment_date == appointment_date)
      errors.add(:appoitnemnt_date, "is already booked")
    end
  end
end

And yes employee_id is in Appointment model

Aucun commentaire:

Enregistrer un commentaire