在index方法中,可以使用includes方法来预加载order_items和product_sku,以消除n + 1查询问题。可以将以下代码添加到index方法中:\n\nruby\ndef index\n @orders = Order.where(user_id: current_user.id).includes(order_items: :product_sku)\nend\n\n\n这将使用includes方法来预加载order_items和product_sku。这样就可以在视图中使用order.order_items和item.product_sku而不会导致额外的查询。

Rails 优化:消除N+1查询问题,提升性能

原文地址: https://www.cveoy.top/t/topic/p1vI 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录