【发布时间】:2017-04-25 22:39:25
【问题描述】:
我的 Rails 应用中有一个查询,如下所示:
# Returns any records whose period intersect the specified time
# @see https://www.postgresql.org/docs/9.3/static/rangetypes.html
# @see https://www.postgresql.org/docs/9.3/static/functions-range.html
# @note The '()' means exclude both sides of the range
#
# @param period_start [DateTime,Time] Start of the range
# @param period_end [DateTime,Time] End of the range
scope :overlapping, ->(period_start, period_end) {
where(
"#{table_name}.period && tsrange(:period_start, :period_end, '()')",
period_start: Time.at(period_start.to_i), # Caps precision to the second
period_end: Time.at(period_end.to_i) # Caps precision to the second
).distinct
}
但是,这里的问题是任何与period_start 和period_end 创建的范围具有相同句点的记录都不会返回。这是为什么?我认为重叠检查是否有任何交叉点?
【问题讨论】:
标签: ruby-on-rails postgresql overlap range-types