When working with date ranges or numeric intervals, the range operator ..
(inclusive) is a powerful and clean tool. In this post, we’ll break down how it works and how to use it effectively in your database queries — along with a comparison to the more verbose alternative.
What is the ..
operator?
In Ruby, ..
creates an inclusive range, meaning it includes both the start and end values.
1
2
(1..5).to_a
# => [1, 2, 3, 4, 5]
Example: Querying with ..
versus Without It
Using the Range Operator ..
In Rails, the ..
operator becomes especially useful when querying records within a time window or a range of values.
Suppose you want to find all Slot
records with start_at
dates up to 30 days from now:
1
2
cutoff = 30.days.from_now
Slot.where(start_at: ..cutoff)
This will generate SQL roughly like:
1
SELECT * FROM slots WHERE start_at <= '2025-06-19 12:00:00';
It’s short, expressive, and very readable.
Without Using the Range Operator
The same query, written without the ..
operator, would look like this:
1
Slot.where('start_at <= ?', 30.days.from_now)
This is perfectly valid, but it’s more verbose and less “Ruby-like.”
Using the ..
operator helps you write cleaner, more idiomatic Ruby — especially when working with time-based queries in Rails.