【发布时间】:2020-04-04 17:55:27
【问题描述】:
我有两种模式:空间和预订。 Space has_many bookings 并且 Booking 有两个日期属性:check_in 和 check_out。
给定一个有效的日期范围,我想显示此范围内的所有可用空间
这是视图:
<%= form_tag spaces_path, method: :get do %>
<%= date_field_tag :query1,
params[:query1],
class: "form-control" %>
<%= date_field_tag :query2,
params[:query2],
class: "form-control" %>
<%= submit_tag "Search", class: "btn" %>
<% end %>
这是空间控制器:
(...)
def index
if params[:query1].present? && params[:query2].present?
query1 = DateTime.parse(params[:query1])
query2 = DateTime.parse(params[:query2])
search = query1..query2
bookings = Booking.all
# returns the bookings that overlaps with the search
overlapping_bookings = bookings.select do |booking|
check_in = booking[:check_in]
check_out = booking[:check_out]
period = check_in..check_out
search.overlaps?(booking.period)
end
# returns the spaces_id of the bookings that overlaps
overlapping_space_ids = overlapping_bookings.select do |overlapping_booking|
overlapping_booking[:space_id]
end
# remove the duplicates
overlapping_space_ids.uniq!
# remove the spaces with bookings that overlap with the search
@spaces = Space.all.reject do |space|
overlapping_space_ids.include? space[:id]
end
else
@spaces = Space.all
end
end
(...)
我认为我的问题的根本原因是我将Active Record Query Object 视为哈希数组,不确定它是否正确。我对此进行了一些研究,但没有找到任何详尽的答案。
【问题讨论】:
标签: ruby-on-rails ruby rails-activerecord