Ir al contenido

Lazy Load In Loop (ARCH6059)

Esta página aún no está disponible en tu idioma.

Key ruby.orm.lazy-load-in-loop · Severity warning · Category orm

Accessing a lazily-loaded relation on each element of an iterated collection triggers N+1 at runtime when eager loading was omitted. The relation must be loaded before the loop.

orders.each do |order|
total += order.customer.discount # lazy-loads customer each iteration
end
orders = orders.includes(:customer)
orders.each do |order|
total += order.customer.discount
end

Preload the relations accessed in the loop with includes to prevent lazy-load triggering.