Aller au contenu

Missing Eager Load (ARCH6051)

Ce contenu n’est pas encore disponible dans votre langue.

Key ruby.orm.missing-eager-load · Severity warning · Category orm

A query that iterates over related data without declaring eager loading is the root cause of N+1. In ActiveRecord the relation must be preloaded with includes/preload/eager_load.

def comment_authors
Comment.recent.map { |c| c.user.email } # user not eager-loaded
end
def comment_authors
Comment.recent.includes(:user).map { |c| c.user.email }
end

Declare the relations accessed inside the loop with includes so they load in a single query.