rubocop / rails-style-guide

A community-driven Ruby on Rails style guide
http://rails.rubystyle.guide
6.47k stars 1.06k forks source link

Add "Finding missing relationship records" rule #320

Closed ydah closed 2 years ago

ydah commented 2 years ago

If you're using Rails 6.1 or higher, prefer where.missing over left_joins and where to find missing relationship records. It can be expressed in a more simplified.

# bad
Post.left_joins(:author).where(authors: { id: nil })

# good
Post.where.missing(:author)
koic commented 2 years ago

It would be better to mention requiring Rails 6.1 or higher. https://github.com/rails/rails/commit/9bfe89e68ee61d9c8ed71ac98c783a7e47bdb608

ydah commented 2 years ago

Indeed it is, I added the supplement that it is for Rails 6.1 or higher.

pirj commented 2 years ago

@koic Please merge on will.

koic commented 2 years ago

Thanks!