Tuesday, 15 May 2012

Ruby Validation When Adding to Database -



Ruby Validation When Adding to Database -

i new in ruby , on lastly step finish project, when i'm trying add together appointment have alter if doc works in time. don't know how :(

it how db works:

in appointment have data_wizyty (visit_date), doctor_id , godzina_wizyty(visit_time) - in adding form.

in schedules have: dzien_tygodnia(day_of_the_week), poczatek_pracy(start_working), koniec_pracy(end_working) , doctors_workplace_id

in doctors_workplace: doctor_id, schedule_id, clinic_id

i want check if doc available in of clinic in choosen date , time :) please help me :) have validated if date , time unique with:

class appointment < activerecord::base validates :doctor_id, uniqueness: { scope: [:data_wizyty, :godzina_wizyty], message: 'ten termin jest juz zajety!' } end

i need check if unique , if doc 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 have :

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

it's have now:

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

you can utilize custom validation. create private method in appointment checks if doc available @ given date/time.

validate :check_doctor_available private def check_doctor_available #your implementation end

take @ this if have doubts write in custom validation method.

ruby-on-rails ruby

No comments:

Post a Comment