jbuilder icon indicating copy to clipboard operation
jbuilder copied to clipboard

N + 1 query when including has_many through associations

Open ptarud opened this issue 6 years ago • 2 comments

It seems jbuilder make SQL query before render it jbuilder. Is not using ActiveRecord::Associations passed from controller correctly.

Example:

# Model
class Product
  has_many :product_sides
  has_many :product_side_images, through: :product_sides
end
# Controller
def index
   @products = Product.includes(:product_side_images).references(:product_side_images)
end

Jbuilder make SQL query for getting each product_side and each product_side_images. (N*M + 1 query)

Calling Product.includes(:product_side_images).references(:product_side_images) and iterating from all it product_side_images from console for example it did just one SQL query.

ptarud avatar May 17 '19 15:05 ptarud

I'm having exactly this issue. Did you find a solution?

LewisYoul avatar Mar 26 '21 17:03 LewisYoul

Having the same issue as well, was anyone able to fix it?

mmenisy avatar Jun 19 '24 04:06 mmenisy