ruby on rails - Eager loading in "both directions" -
eager loading not work expect.
i have products has_many variants, , of course each variant belongs_to product.
i use code load product , variants:
products = product.includes(:variants)
this works: products , variants loaded 2 queries. however, product of each variant not loaded, following code causes sql-query:
puts products[0].variants[0].product.title
why that, , how can fix it? suppose product.includes(:variants => :product)
work, causes 1 big , unnecessary sql-query, since product-data available.
active record eager loading association on level you've specified. in point of view, variant.product
treated level of association. so, if want eager loading it, you'd have do:
products = product.includes({:variants => :product})
Comments
Post a Comment