Orm N Plus One Query (ARCH6079)
Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.
Key ruby.performance.orm-n-plus-one · Severity error · Category performance
Looping over a query result and accessing each item’s relationship fires a separate query per record (N+1). Eager-load the relationship with includes.
Incorrect
Section titled “Incorrect”def author_names(posts) Post.where(published: true).map do |post| post.author.name endendCorrect
Section titled “Correct”def author_names(posts) Post.where(published: true).includes(:author).map do |post| post.author.name endendEager-load related records with includes/preload when you access them inside a loop.