我的模型设置方式:
以下查询似乎正常工作,因为我想在db中急切加载第19个Order对象的声明关联:
# Load order, and its charged_amounts & refunds
@order = Order.includes(:charged_amounts, :refunds).find(19)
=>
Order Load (0.2ms) SELECT "orders".* FROM "orders" WHERE "orders"."id" = ? LIMIT 1 [["id", 19]]
ChargedAmount Load (0.3ms) SELECT "charged_amounts".* FROM "charged_amounts" WHERE "charged_amounts"."order_id" IN (19)
Refund Load (0.3ms) SELECT "refunds".* FROM "refunds" WHERE "refunds"."order_id" IN (19)
# Load order, its type_logistics, and the charged_amounts & refunds that belong to each type_logistic
@order = Order.includes(type_logistics:[:charged_amounts, :refunds]).find(19)
=>
Order Load (0.1ms) SELECT "orders".* FROM "orders" WHERE "orders"."id" = ? LIMIT 1 [["id", 19]]
TypeLogistic Load (1.1ms) SELECT "type_logistics".* FROM "type_logistics" WHERE "type_logistics"."order_id" IN (19)
ChargedAmount Load (0.3ms) SELECT "charged_amounts".* FROM "charged_amounts" WHERE "charged_amounts"."type_logistic_id" IN (26)
Refund Load (0.2ms) SELECT "refunds".* FROM "refunds" WHERE "refunds"."type_logistic_id" IN (26)
# Load order, its rental_items, and the charged_amounts & refunds that belong to each rental_item
@order = Order.includes(rental_items: [:charged_amounts, :refunds]).find(19)
=>
Order Load (0.3ms) SELECT "orders".* FROM "orders" WHERE "orders"."id" = ? LIMIT 1 [["id", 19]]
RentalItem Load (0.2ms) SELECT "rental_items".* FROM "rental_items" WHERE "rental_items"."order_id" IN (19)
ChargedAmount Load (0.2ms) SELECT "charged_amounts".* FROM "charged_amounts" WHERE "charged_amounts"."rental_item_id" IN (27, 28)
Refund Load (0.1ms) SELECT "refunds".* FROM "refunds" WHERE "refunds"."rental_item_id" IN (27, 28)
但挑战在于我想急切加载所有这些:订单的charge_amounts和退款,以及所有的rental_items和type_logistics以及每个rental_item和type_logistic的charge_amounts和退款。
但是当我将查询串在一起时,看起来第二组关联(type_logistics及其charge_amounts和refunds)根本不会根据查询加载。
@order = Order.includes(:charged_amounts, :refunds, rental_items: [:charged_amounts, :refunds], type_logistics:[:charged_amounts, :refunds]).find(19)
=>
Order Load (0.2ms) SELECT "orders".* FROM "orders" WHERE "orders"."id" = ? LIMIT 1 [["id", 19]]
ChargedAmount Load (0.2ms) SELECT "charged_amounts".* FROM "charged_amounts" WHERE "charged_amounts"."order_id" IN (19)
Refund Load (0.2ms) SELECT "refunds".* FROM "refunds" WHERE "refunds"."order_id" IN (19)
RentalItem Load (0.2ms) SELECT "rental_items".* FROM "rental_items" WHERE "rental_items"."order_id" IN (19)
ChargedAmount Load (0.1ms) SELECT "charged_amounts".* FROM "charged_amounts" WHERE "charged_amounts"."rental_item_id" IN (27, 28)
Refund Load (0.1ms) SELECT "refunds".* FROM "refunds" WHERE "refunds"."rental_item_id" IN (27, 28)
但订单上的charge_amounts和退款没有任何其他关联,所以它们不是任何东西的“关键”。有什么帮助吗?
答案 0 :(得分:1)
我在之前的项目中遵循的方法是:
Order.eager_load(:charged_amounts, :refunds)
.eager_load(rental_items: [:charged_amounts, :refunds])
.eager_load(type_logistics: [:charged_amounts, :refunds] )
.find(19)
我不知道这可能是解决方案。另请注意,我使用的是AR.eager_load
方法。随意交换AR.includes