0

I am really new in Ruby and I am on the last step to finish my project, when I'm trying to add appointment I have to change if doctor works in that time. I don't know how to do this :(

It is how my db works:

In appointment I have data_wizyty (visit_date), doctor_id and godzina_wizyty(visit_time) - it is in my adding form.

In schedules I have: dzien_tygodnia(day_of_the_week), poczatek_pracy(start_working), koniec_pracy(end_working) and doctors_workplace_id

In doctors_workplace: doctor_id, schedule_id, clinic_id

I want to check if doctor is available in any of the clinic in choosen date and time :) Please help me with this :) I have already validated if date and time is unique with:

class Appointment < ActiveRecord::Base
    validates :doctor_id, uniqueness: { scope: [:data_wizyty, :godzina_wizyty], message: 'Ten termin jest juz zajety!' }
end

I need to check if it is unique and if doctor works.

Appointment:

class Appointment < ActiveRecord::Base
    validates :doctor_id, uniqueness: { scope: [:data_wizyty, :godzina_wizyty], message: 'Ten termin jest juz zajety!' }
    after_initialize :aInit
    after_save :aSave

    belongs_to :patient
    belongs_to :doctor
    belongs_to :schedule
    belongs_to :refferal
    belongs_to :clinic

    has_many :employees

    include MultiStepModel

    def self.total_steps
        3
    end

    def aInit 
        @wymaga_Potwierdzenia = true 
    end

    def aSave
        if self.refferal_id == nil
            @potwierdzona = false
        else
            @potwierdzona = true
        end
        if self.wymaga_Potwierdzenia == false
            @potwierdzona = true
        end
    end

end

Schedule:

class Schedule < ActiveRecord::Base

    has_many :appointments
    belongs_to :clinic
    belongs_to :doctors_workplace

    def full_schedule
        "#{dzien_tygodnia} : #{poczatek_pracy} - #{koniec_pracy}"
    end

end

Doctors_workplace:

class DoctorsWorkplace < ActiveRecord::Base

has_many :schedules
belongs_to :doctor
belongs_to :clinic_surgery

end

Now I have something like this :

def check_doctor_available
        if Schedule.where(doctor: doctor, dzien_tygodnia: data_wizyty.wday)
               .where('poczatek_pracy < ? and koniec_pracy > ?', godzina_wizyty, godzina_wizyty).empty?
      self.errors.add(:doctor, message: 'nie pracuje w tym terminie!')
    end

It's what I have now:

def check_doctor_available
        if DoctorsWorkplace.where(doctor_id: doctor_id) and
           Schedule.where(doctors_workplace_id: ????, dzien_tygodnia: data_wizyty.wday)
               .where('poczatek_pracy < ? and koniec_pracy > ?', godzina_wizyty, godzina_wizyty).empty?
      self.errors.add(:doctor, message: 'nie pracuje w tym terminie!')
    end
2
  • Can you show us what the 3 models & their associations look like? Commented Jun 22, 2014 at 17:27
  • @Anthony question updated Commented Jun 22, 2014 at 17:45

1 Answer 1

1

You can use a custom validation. Create a private method in appointment that checks if the doctor is available at the given date/time.

validate :check_doctor_available

private
def check_doctor_available
  #your implementation
end

Take a look at this if you have any doubts what to write in your custom validation method.

Sign up to request clarification or add additional context in comments.

5 Comments

I think I have to check which day is choosen(Monday,Tuesday etc...) and then chech all doctor_workplace for all this doctor's records, then check if choosen day is in Schedules with this doctors_workplace_id is dzien_tygodnia(but how with the day names in db...) later check the time between poczatek_pracy and koniec_pracy :) But I have no idead how to code this :P
You got it. Cannot give you the implementation though.
How check if something equal something else ?
please check my updated question and correct my mistakes, I don't know what put there - "???" – smiaro 5 mins ago

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.