我在我的日程应用程序中拥有一个名为 Event model
的模型,其中包含 start_at
时间和 end_at
时间,并且我希望在保存之前验证重叠时间。
我是在Cloud9上创建我的Rails应用程序的。
我的视图图像如下所示:
Day1
07:00 - 07:20 event1
10:30 - 11:30 event2
15:40 - 16:10 event3
[add event button]
Day2
08:15 - 09:05 event4
12:08 - 13:04 event5
14:00 - 14:25 event6
[add event button]
[save schedule button]
start_at
时间和end_at
时间可以同时更改和添加。
我想要做的是,如果我尝试为Day1
添加(或更改为)07:05 - 07:30
,例如为Day2
添加13:50 - 14:30
等,则显示错误。
例如:
app_development=# select * from events;
id | start_at | end_at | title | detail | schedule_id | created_at | updated_at
----+----------+----------+--------+--------+-----------------+----------------------------+----------------------------
1 | 07:00:00 | 07:20:00 | event1 | | 1 | 2016-04-12 05:28:44.166827 | 2016-04-12 12:52:07.682872
2 | 10:30:00 | 11:30:00 | event2 | | 1 | 2016-04-12 05:28:44.17747 | 2016-04-12 12:52:07.689934
3 | 15:40:00 | 16:10:00 | event3 | | 1 | 2016-04-12 05:29:07.5005 | 2016-04-12 12:52:07.693477
我在表格上方添加了
07:05 - 07:30
,但验证不起作用。虽然我曾经提出类似的问题,但被建议使用postgresql而不是sqlite3。
所以我成功配置了postgresql,但结果仍然相同。如果您能告诉我如何检查和显示错误,我将不胜感激。 schema.rb
create_table "events", force: :cascade do |t|
t.time "start_at"
t.time "end_at"
t.string "title"
t.integer "room_id"
...
create_table "rooms", force: :cascade do |t|
t.string "room"
t.integer "schedule_id"
...
create_table "schedules", force: :cascade do |t|
t.string "title"
t.integer "user_id"
t.date "departure_date"
...
给出以下模型:
class Event < ActiveRecord::Base
belongs_to :room, inverse_of: :events
has_one :schedule, autosave: false, through: :room
...
validate :cannot_overlap_another_event
def cannot_overlap_another_event
range = Range.new start_at, end_at
overlaps = Event.exclude_self(id).in_range(range)
overlap_error unless overlaps.empty?
end
scope :in_range, -> range {
where('(start_at BETWEEN ? AND ?)', range.first, range.last)
}
scope :exclude_self, -> id { where.not(id: id) }
def overlap_error
errors.add(:overlap_error, 'There is already an event scheduled in this hour!')
end
class Schedule < ActiveRecord::Base
belongs_to :user
has_many :rooms, inverse_of: :schedule
accepts_nested_attributes_for :rooms, allow_destroy: true
...
class Room < ActiveRecord::Base
belongs_to :schedule, inverse_of: :rooms
has_many :events, inverse_of: :room
accepts_nested_attributes_for :events, allow_destroy: true
...
_schedule_form.html.erb
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :title %>
<%= f.text_field :title, class: 'form-control' %>
<br>
<%= f.label :departure_date %>
<div class="input-group date" id="datetimepicker">
<%= f.text_field :departure_date, :value => (f.object.departure_date if f.object.departure_date), class: 'form-control' %>
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
<script type="text/javascript">
$(function () {
$('#datetimepicker').datetimepicker({format:'YYYY-MM-DD'});
});
</script>
<br>
<div id="room">
<%= f.simple_fields_for :rooms do |a| %>
<div id="room_<%= a.object.object_id %>">
<p class="day-number-element-selector"><b>Day <%= a.index.to_i + 1 %></b></p>
<%= a.simple_fields_for :events do |e| %>
<span class="form-inline">
<p>
<%= e.input :start_at, label: false %> -
<%= e.input :end_at, label: false %>
</p>
</span>
<%= e.input :title, label: false %>
<% end %>
</div>
<%= a.link_to_add "Add event", :events, data: {target: "#room_#{a.object.object_id}"}, class: "btn btn-primary" %>
<%= a.input :room %>
<% end %>
</div>
如果您能告诉我如何检查和显示错误,我将不胜感激。
编辑
编辑如下:
event.rb
scope :in_range, -> range {
where('(start_at BETWEEN ? AND ? OR end_at BETWEEN ? AND ?) OR (start_at <= ? AND end_at >= ?)', range.first, range.last, range.first, range.last, range.first, range.last)
}
尽管看起来似乎有效,但是当我添加一个不同日期的
event
时,这个验证就无法正常工作,如下所示id=8
。(请参阅created_at
和updated_at
)。
app_development=# select * from events;
id | start_at | end_at | title | detail | room_id | created_at | updated_at
----+----------+----------+--------+--------+-----------------+----------------------------+----------------------------
1 | 07:00:00 | 07:20:00 | event1 | | 1 | 2016-04-12 05:28:44.166827 | 2016-04-12 12:52:07.682872
2 | 10:30:00 | 11:30:00 | event2 | | 1 | 2016-04-12 05:28:44.17747 | 2016-04-12 12:52:07.689934
3 | 15:40:00 | 16:10:00 | event3 | | 1 | 2016-04-12 05:29:07.5005 | 2016-04-12 12:52:07.693477
8 | 07:05:00 | 07:10:00 | event4 | | 1 | 2016-04-15 21:37:58.569868 | 2016-04-15 21:39:27.956737
where("? BETWEEN start_at AND end_at OR ? BETWEEN start_at AND end_at OR start_at BETWEEN ? AND ?", range.first, range.last, range.first, range.last)
。 - pozs