-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
24 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Find records missing an association with `where.missing` | ||
|
||
In rails 6.1 you can find all records missing an association without `left_joins` | ||
|
||
```ruby | ||
class Account < ApplicationRecord | ||
has_many :recovery_email_addresses | ||
end | ||
|
||
# Before Rails 6.1 | ||
Account.left_joins(:recovery_email_addresses).where(recovery_email_addresses: { id: nil }) | ||
|
||
# After Rails 6.1 | ||
Account.where.missing(:recovery_email_addresses) | ||
``` | ||
|
||
- [link](https://boringrails.com/tips/activerecord-where-missing-associations) | ||
|
||
There is also rubocop rule for that [Rails/WhereMissing](https://docs.rubocop.org/rubocop-rails/cops_rails.html#railswheremissing) | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters