I try to create validation that checks times overlapping.
scope :overlapping_activity, ->(activity) { where("(start_on, end_on) OVERLAPS (?, ?)",
activity.start_on, activity.end_on }
validate :not_overlapping_activity
def not_overlapping_activity
if Activity.overlapping_activity(self).any?
errors.add(:base, "Zajęcie w tym okresie koliduje z innymi.")
end
end
Error:
ERROR: function pg_catalog.overlaps(time without time zone, time without time zone, unknown, unknown) is not unique
LINE 1: ...NT(*) FROM "activities" WHERE ((start_on, end_on) OVERLAPS (...
I found this topic: Rails 3.1: Querying Postgres for records within a time range, but I cannot use it in my solution. While I try add timestamps in scope, it shows another syntax errors. Could someone show me how to properly resolve my issue?
How I tried use timestamp:
scope :overlapping_activity, ->(activity) { where("(start_on::timestamp, end_on::timestamp) OVERLAPS (?, ?)",
timestamp activity.start_on, timestamp activity.end_on) }