I am new in Ruby. I have form which creates new appointment (I save there doctor_id
, patient_id
, date
, time
) I have also schedules in database (doctor_id
in schedule is FK
, there are dayofweek
, starttime
, endtime
) and now I want to check if doctor is available in chosen day and time and if there are no appointment with this date and time. If I can't add and error message.
我是Ruby的新手。我有创建新约会的表格(我保存在那里doctor_id,patient_id,日期,时间)我也有数据库的时间表(医生时间表是FK,有周日,开始时间,结束时间)现在我想检查医生是否可用在选定的日期和时间,如果没有约会这个日期和时间。如果我无法添加和错误消息。
Translate for your better understanding:
翻译以便您更好地理解:
-dzien_tygodnia - day_of_the_week;
-data_wizyty - visit_date;
-godzina_wizyty - visit_time;
-poczatek_pracy - start_working;
-koniec_pracy - end_working.
My _form.html.erb
:
我的_form.html.erb:
<%= form_for(@appointment) do |f| %>
<% if @appointment.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@appointment.errors.count, "error") %> prohibited this appointment from being saved:</h2>
<ul>
<% @appointment.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :data_wizyty %><br />
<%=
options = { start_year: 2.year.from_now.year,
end_year: 2013,
include_blank: true,
default: nil }
f.date_select :data_wizyty, options
%>
<!--<input type="text" data-behaviour='datepicker' :data_wizyty > -->
</div>
<div class="field">
<%= f.label :godzina_wizyty %><br />
<%= f.time_select :godzina_wizyty %>
</div>
<div class="field">
<%= f.hidden_field :doctor_id, :value => Doctor.find(session[:current_doctor_id2]).id %>
</div>
<div class="field">
<%= f.hidden_field :patient_id, :value => Patient.find(session[:current_patient_id]).id %>
</div>
<div class="actions">
<%= submit_tag "Utworz wizyte" %>
</div>
<% end %>
appointment.rb
:
appointment.rb:
class Appointment < ActiveRecord::Base
validates :doctor_id, uniqueness: { scope: [:data_wizyty, :godzina_wizyty], message: 'Ten termin jest juz zajety!' }
validate :doctor_is_scheduled
attr_accessible :data_wizyty, :doctor_id, :godzina_wizyty, :notatka, :objawy_choroby, :patient_id
belongs_to :patient
belongs_to :doctor
belongs_to :schedule
belongs_to :refferal
has_many :employees
def doctor_is_scheduled
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
end
end
Could you tell me which files should be included?
你能告诉我应该包含哪些文件吗?
1 个解决方案
#1
3
What you're looking for here are validations. Give that Rails Guide a read, because it'll be incredibly helpful for you. As for your question, you should be able use the following:
您在这里寻找的是验证。给Rails指南一个阅读,因为它对你非常有帮助。至于你的问题,你应该能够使用以下内容:
class Appointment < ActiveRecord::Base
validates :doctor_id, uniqueness: { scope: [:date, :time], 'already has an appointment at that time' }
validate :doctor_is_scheduled
def doctor_is_scheduled
if Schedule.where(doctor: doctor, dayofweek: date.wday)
.where('starttime < ? and endtime > ?', time, time).empty?
self.errors.add(:doctor, 'is not scheduled to be working at that time')
end
end
end
The first line (validates :doctor_id, uniqueness:
) checks that for the given date and time, the doctor is unique, i.e. the doctor won't be double-booked. The doctor_is_scheduled
method checks to see for the given doctor and day of the week, the doctor has a shift (between starttime
and endtime
) scheduled.
第一行(验证:doctor_id,uniqueness :)检查在给定的日期和时间,医生是唯一的,即医生不会被双重预订。 doctor_is_scheduled方法检查以查看给定的医生和一周中的某一天,医生有预定的班次(在开始时间和结束时间之间)。
#1
3
What you're looking for here are validations. Give that Rails Guide a read, because it'll be incredibly helpful for you. As for your question, you should be able use the following:
您在这里寻找的是验证。给Rails指南一个阅读,因为它对你非常有帮助。至于你的问题,你应该能够使用以下内容:
class Appointment < ActiveRecord::Base
validates :doctor_id, uniqueness: { scope: [:date, :time], 'already has an appointment at that time' }
validate :doctor_is_scheduled
def doctor_is_scheduled
if Schedule.where(doctor: doctor, dayofweek: date.wday)
.where('starttime < ? and endtime > ?', time, time).empty?
self.errors.add(:doctor, 'is not scheduled to be working at that time')
end
end
end
The first line (validates :doctor_id, uniqueness:
) checks that for the given date and time, the doctor is unique, i.e. the doctor won't be double-booked. The doctor_is_scheduled
method checks to see for the given doctor and day of the week, the doctor has a shift (between starttime
and endtime
) scheduled.
第一行(验证:doctor_id,uniqueness :)检查在给定的日期和时间,医生是唯一的,即医生不会被双重预订。 doctor_is_scheduled方法检查以查看给定的医生和一周中的某一天,医生有预定的班次(在开始时间和结束时间之间)。