Skip to content

N + 1 query when including has_many through associations #463

Open
@ptarud

Description

@ptarud

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions