Image for TWIL post depicting ActiveRecord range query examples in Ruby on Rails, enhancing database search efficiency.

Welcome to another insightful installment of TWIL; your weekly micro-lesson in complex software development! This week, Katie enhances our ActiveRecord arsenal by introducing New Ranges for LT/GT in ActiveRecord Queries. Discover how Ruby and Rails' evolving syntax simplifies database search criteria far beyond simple equalities, allowing for elegant and error-resistant range-based queries. Dive into Katie's latest guide to learn how ActiveRecord has shed cumbersome raw SQL for the sophistication of endless ranges, streamlining the quest for records by attributes greater or lesser than a dynamic value, all within the elegant embrace of Ruby's syntactic sugar.

New ranges for LT/GT in ActiveRecord Queries

Problem?

Using the ActiveRecord query interface, retrieving objects where some property is equal to a given value looks like this:

User.where(active: true)

This will return user records that have an active value of true.

This can be used for retrieving records where the property matches a set of values as well:

User.where(favorite_animal: ['cat', 'dog'])

This will return user records that have a favorite_animal value of either cat or dog.

For greater than/less than queries, though, previous versions of Ruby/Rails unfortunately required writing a little bit of SQL:

User.where('users.id > ?', 10)

This would return user records with an id greater than 10.

User.where('users.last_logged_in_at < ?', 1.day.ago)

This would return user records with a last_logged_in_at timestamp earlier than one day ago.

This is a little bit gross, prone to human error (or, at the very least, inconsistency), and not very Rails-y.

Solution!

As of Ruby 2.6, we can use endless ranges (and beginless ranges as of Ruby 2.7).

1..10

1...10

To rewrite the above greater than/less than queries using the newly-introduced open ranges:

User.where(id: 10...)

This would return user records with an id greater than 10.

User.where(last_logged_in_at: ...1.day.ago)

This would return user records with a last_logged_in_at timestamp earlier than one day ago.

Note: For inclusive ranges (i.e., <= / >= queries), the syntax is .. instead of exclusive ranges' ...

Resources

  • Ruby
  • Rails
Katie Linero's profile picture
Katie Linero

Senior Software Engineer

Related Posts

Illustration of a small, determined knight in weathered medieval armor and a bucket helmet, wearing a tattered red cape, striding across barren cracked earth with sword drawn, surrounded by a swirling cloud of scattered wooden alphabet letters representing Token Guard, a GitHub Action that monitors and guards against token context bloat in AI coding agent workflows by counting the tokens in LLM instruction files committed to repositories."
February 10, 2026 • Frank Valcarcel

Token Guard: Keeping Your Agent Context Lean in CI

Token Guard is a GitHub Action that counts tokens in your agent context files and enforces limits in CI. Here’s why we check agent context into our repos, and why keeping it lean matters for team collaboration.

Software engineer reviewing code during a code audit, analyzing source code on a monitor alongside documentation in a modern office environment.
February 18, 2025 • Frank Valcarcel

How We Review and Audit Software

A thorough code audit goes beyond automated scanners. Here is how we evaluate security, architecture, maintainability, compliance, and what you can expect when you work with us.

Let's work together

Tell us about your project and how Cuttlesoft can help. Schedule a consultation with one of our experts today.

Contact Us