1

我了解 Rails 以 UTC 存储时间。

我有 DateTime 的 out_date 和 in_date 实例

我有这个查询:

reservations = Reservation.where("bookable_id = :bookable_id AND bookable_type = :bookable_type AND status <> :status AND ((:out_date >= check_out_date AND  :out_date <= check_in_date) OR (:in_date <= check_in_date AND :in_date >= check_out_date) OR (:out_date <= check_in_date AND :in_date >= check_in_date))", :bookable_id => params[:bookable_id], :bookable_type => params[:bookable_type], :status => Reservation::STATUS_CHECKED_IN, :out_date => out_date, :in_date => in_date)

即使我应该得到一个返回元组,我总是得到一个空集。

我试过这些变种:

out_date.utc

out_date.utc.to_s(:db)

似乎没有任何工作。如何构造这个查询?

控制器代码:

  in_date = DateTime.strptime params[:checkin], "%Y-%m-%d %H:%M"
  out_date = DateTime.strptime params[:checkout], "%Y-%m-%d %H:%M"

  #check collision with non-recurring reservations
  reservations = Reservation.where("bookable_id = :bookable_id AND bookable_type = :bookable_type AND status <> :status AND ((:out_date >= check_out_date AND  :out_date <= check_in_date) OR (:in_date <= check_in_date AND :in_date >= check_out_date) OR (:out_date <= check_in_date AND :in_date >= check_in_date))", :bookable_id => params[:bookable_id], :bookable_type => params[:bookable_type], :status => Reservation::STATUS_CHECKED_IN, :out_date => out_date, :in_date => in_date)
  logger.info(reservations)
  if !reservations.empty?
    @error_message = "This Asset has already been reserved for those dates"
    return
  end

同样在 Rails 控制台上,更简单的查询失败:

1.9.3p0 :007 > Reservation.find_by_id 31
  Reservation Load (0.7ms)  SELECT `reservations`.* FROM `reservations` WHERE `reservations`.`id` = 31 LIMIT 1
 => #<Reservation id: 31, bookable_id: 11, bookable_type: "Asset", user_id: 1, check_out_date: "2012-07-07 08:00:00", check_in_date: "2012-07-07 10:00:00", notes: "rec", status: "Ready", is_recurring: true, repeat_count: 5>


1.9.3p0 :009 > Reservation.where " ? >= check_out_date AND ? <= check_in_date",DateTime.new(2012,7,7,9),DateTime.new(2012,7,7,9)
  Reservation Load (0.5ms)  SELECT `reservations`.* FROM `reservations` WHERE ( '2012-07-07 09:00:00' >= check_out_date AND '2012-07-07 09:00:00' <= check_in_date)
 => [] 
4

2 回答 2

1

我阅读了更新的代码,显然它是正确的。确保out_dateandin_dateDateTime对象而不是空对象。

您不需要格式化查询,它应该为您处理。

如果要调试查询 SQL,可以使用.to_sql

reservations = Reservation.where("...") # your query
puts reservation.to_sql
# => "SELECT ..."

打印查询并检查值的格式是否正确。

于 2012-07-04T09:04:58.347 回答
0

错误是因为数据库时间存储在UTC和本地时间不同,不满足查询条件。

于 2012-07-04T19:42:00.350 回答