rails / jbuilder

Jbuilder: generate JSON objects with a Builder-style DSL
MIT License
4.34k stars 440 forks source link

N + 1 query when including has_many through associations #463

Open ptarud opened 5 years ago

ptarud commented 5 years ago

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.

LewisYoul commented 3 years ago

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

mmenisy commented 5 months ago

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