Skip to content

Commit

Permalink
Add rails 016
Browse files Browse the repository at this point in the history
  • Loading branch information
kopvlad committed Jan 29, 2024
1 parent 17075f8 commit 6707b4b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
23 changes: 23 additions & 0 deletions docs/ruby_on_rails/016.md
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)




1 change: 1 addition & 0 deletions docs/ruby_on_rails/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Welcome to Ruby on Rails page. Please open menu and enjoy.
- [Railsdiff](ruby_on_rails/013.md)
- [find vs find_by](ruby_on_rails/014.md)
- [preload vs includes](ruby_on_rails/015.md)
= [missing an association with where.missing](ruby_on_rails/016.md)

## Links

Expand Down

0 comments on commit 6707b4b

Please sign in to comment.