Skip to content

Commit d63886d

Browse files
committed
Add "Finding missing relationship records" rule
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. ```ruby # bad Post.left_joins(:author).where(authors: { id: nil }) # good Post.where.missing(:author) ```
1 parent 43a9832 commit d63886d

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

README.adoc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,19 @@ User.where("id != ?", id)
938938
User.where.not(id: id)
939939
----
940940

941+
=== Finding missing relationship records [[finding-missing-relationship-records]]
942+
943+
If you're using Rails 6.1 or higher, use https://api.rubyonrails.org/classes/ActiveRecord/QueryMethods/WhereChain.html#method-i-missing[where.missing] to find missing relationship records.
944+
945+
[source,ruby]
946+
----
947+
# bad
948+
Post.left_joins(:author).where(authors: { id: nil })
949+
950+
# good
951+
Post.where.missing(:author)
952+
----
953+
941954
=== Order by `id` [[order-by-id]]
942955

943956
Don't use the `id` column for ordering.

0 commit comments

Comments
 (0)