---
title: "Rails 6.1 adds where.associated to check association presence"
description: "Rails 6.1 adds where.associated to check association presence"
canonical_url: "https://www.bigbinary.com/blog/rails-6-1-adds-where-associated-to-check-association-presence"
markdown_url: "https://www.bigbinary.com/blog/rails-6-1-adds-where-associated-to-check-association-presence.md"
---

# Rails 6.1 adds where.associated to check association presence

Rails 6.1 adds where.associated to check association presence

- Author: Nithin Krishna
- Published: December 18, 2020
- Categories: Rails, Rails 6.1

Rails 6.1 simplifies how to check whether an association exists by adding a new
`associated` method.

Let's see an example of it.

```ruby
class Account < ApplicationRecord
  has_many :users, -> { joins(:contact).where.not(contact_id: nil) }
end
```

This will return all users with contacts. If we rephrase that sentence then we
can say that "this will return all users who are associated with contacts".

Let's see how we can do the same with the new `associated` method.

```ruby
class Account < ApplicationRecord
  has_many :users, -> { where.associated(:contact) }
end
```

Now we can see that the usage of `associated` decreases some of the syntactic
noise we saw in the first example. This method is essentially a syntactic sugar
over the `inner_joins(:contact)`.

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

## Links

- [Human page](https://www.bigbinary.com/blog/rails-6-1-adds-where-associated-to-check-association-presence)
