Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

N + 1 query when including has_many through associations #463

Open
ptarud opened this issue May 17, 2019 · 2 comments
Open

N + 1 query when including has_many through associations #463

ptarud opened this issue May 17, 2019 · 2 comments

Comments

@ptarud
Copy link

ptarud commented May 17, 2019

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
Copy link

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

@mmenisy
Copy link

mmenisy commented Jun 19, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants