我是Ruby的新手。我有创建新约会的表单(我保存doctor_id
,patient_id
,date
,time
)我还有数据库中的日程安排(doctor_id
日程安排是FK
,有dayofweek
,starttime
,endtime
),现在我想查看医生是否在所选日期和时间可用,以及是否没有约会此日期和时间。如果我无法添加和错误消息。我的问题是我得到错误:
ActiveRecord::StatementInvalid in AppointmentsController#create
SQLite3::SQLException: no such column: schedules.doctor: SELECT COUNT(*) FROM "schedules" WHERE "schedules"."doctor" = 1 AND "schedules"."dzien_tygodnia" = 3 AND (poczatek_pracy < '2014-06-18 08:53:00.000000' and koniec_pracy > '2014-06-18 08:53:00.000000')
在日程安排和预约中,doctor_id是FK!
翻译以便您更好地理解:
-dzien_tygodnia - day_of_the_week;
-data_wizyty - visit_date;
-godzina_wizyty - visit_time;
-poczatek_pracy - start_working;
-koniec_pracy - end_working.
我的_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
:
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
预约中的控制器/创建操作:
def create
@appointment = Appointment.new(params[:appointment])
respond_to do |format|
if @appointment.save
format.html { redirect_to @appointment, notice: 'Appointment was successfully created.' }
format.json { render json: @appointment, status: :created, location: @appointment }
else
format.html { render action: "new" }
format.json { render json: @appointment.errors, status: :unprocessable_entity }
end
end
end
你能告诉我应该包含哪些文件吗?
答案 0 :(得分:0)
看起来你需要更改你的where子句,因为它试图在日程表上使用不存在的医生专栏
Schedule.where(doctor_id: doctor, dzien_tygodnia: data_wizyty.wday)
更新
好的,所以你在新版本中使用@schedules
但是如果它失败了它就不再那样了,所以你需要添加它来创建它。
def create
@appointment = Appointment.new(params[:appointment])
respond_to do |format|
if @appointment.save
format.html { redirect_to @appointment, notice: 'Appointment was successfully created.' }
format.json { render json: @appointment, status: :created, location: @appointment }
else
format.html do
@schedules = Schedule.all #or whatever query you do for new
render action: "new"
end
format.json { render json: @appointment.errors, status: :unprocessable_entity }
end
end
end