---
title: "Rails 6.1 adds support for where with a comparison operator"
description: "Rails 6.1 adds support for where with a comparison operator"
canonical_url: "https://www.bigbinary.com/blog/rails-6-1-adds-support-for-where-with-comparison-operator"
markdown_url: "https://www.bigbinary.com/blog/rails-6-1-adds-support-for-where-with-comparison-operator.md"
---

# Rails 6.1 adds support for where with a comparison operator

Rails 6.1 adds support for where with a comparison operator

- Author: Abhay Nikam
- Published: July 14, 2020
- Categories: Rails 6.1, Rails

**_Please note that the PR discussed in this blog was
[reverted](https://github.com/rails/rails/issues/41271)._**

Rails 6.1 adds support to comparison operator in the `where` clause. The four
comparison operators supported are:

- Greater than (>).
- Greater than equal to (>=).
- Less than (<).
- Less than equal to (<=).

The comparison operator is also supported by the finder methods in ActiveRecord
which internally uses where clause, for example: `find_by`, `destroy_by`,
`delete_by`.

The new style for comparisons has to follow advantages:

- The `where` clause with the comparison operator doesn't raise an exception
  when `ActiveRecord::Relation` uses ambiguous column name.
- The `where` clause with the comparison operator handle proper precision of the
  database columns.

Before Rails 6.1, to add a condition with comparison in where clause, we had to
add raw SQL notation.

#### Rails 6.0.0

```ruby
>> Post.where("DATE(published_at) > DATE(?)", Date.today)
# => <ActiveRecord::Relation [...]>

>> Post.find_by("likes < ?", 10)
# => <ActiveRecord::Relation [...]>

# Following query on execution would raise exception.
>> Post.joins(:comments).where("likes > 10")
# => ambiguous column name: id
```

#### Rails 6.1.0

```ruby
>> Post.where("published_at >": Date.today)
# => <ActiveRecord::Relation [...]>

>> Post.find_by("likes <": 10)
# => <ActiveRecord::Relation [...]>

# Following query on execution would NOT raise exception.
>> Post.joins(:comments).where("likes >": 10)
# => <ActiveRecord::Relation [...]>
```

Check out the [pull request](https://github.com/rails/rails/pull/39613) for more
details on this.

## Links

- [Human page](https://www.bigbinary.com/blog/rails-6-1-adds-support-for-where-with-comparison-operator)
