每个用户rails的日期重叠唯一

时间:2017-11-21 13:04:29

标签: ruby-on-rails

我读了这个问题Date range overlap per user rails

如何使用以下代码验证每位用户:

def no_reservation_overlap
  if (Reservation.overlapping(date_start, date_end).any?)
     errors.add(:date_end, 'it overlaps another reservation')
  end
end

我使用下面的代码作为答案,但它没有验证日期之间的冲突

预订6月2日至3日创建另一个预订1到5,它创建了我如何防止这种情况发生?

validate :no_reservation_overlap


def no_reservation_overlap
  if (Reservation.where("(? BETWEEN date_start AND date_end OR ? BETWEEN date_start AND date_end) AND user_id = ?", self.date_start, self.date_end, self.user_id).any?)
     errors.add(:date_end, 'it overlaps another reservation')
  end
end

0 个答案:

没有答案