ActiveRecord 包含查询

ActiveRecord includes query

产品有变体。 我正在尝试获取具有 cost_currency 的变体:"USD" 变体可能如下所示: Spree::Variant id: 22, cost_currency: "USD">

Spree::Product.includes('variants') returns 很多变体,但如果我尝试仅过滤具有 cost_currency = true 的变体,则会失败。

这是我所做的:

2.2.3 :077 > Spree::Product.includes('variants')
                 .where( variants: { cost_currency: "USD" })

但这会引发 ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: variants.cost_currency 错误,我不明白为什么。

完整堆栈跟踪:

  SQL (0.5ms)  SELECT "spree_products"."id" AS t0_r0, "spree_products"."name" AS t0_r1, "spree_products"."description" 
  AS t0_r2, "spree_products"."available_on" AS t0_r3, "spree_pro
ducts"."deleted_at" AS t0_r4, "spree_products"."slug" AS t0_r5, "spree_products"."meta_description" AS t0_r6, "spree_products"."meta_keywords" AS t0_r7, "spree_products"."tax_catego
ry_id" AS t0_r8, "spree_products"."shipping_category_id" AS t0_r9, "spree_products"."created_at" AS t0_r10, "spree_products"."updated_at" AS t0_r11, "spree_products"."promotionable"
 AS t0_r12, "spree_products"."meta_title" AS t0_r13, "spree_variants"."id" AS t1_r0, "spree_variants"."sku" AS t1_r1, "spree_variants"."weight" AS t1_r2, "spree_variants"."height" A
S t1_r3, "spree_variants"."width" AS t1_r4, "spree_variants"."depth" AS t1_r5, "spree_variants"."deleted_at" AS t1_r6, "spree_variants"."is_master" AS t1_r7, "spree_variants"."produ
ct_id" AS t1_r8, "spree_variants"."cost_price" AS t1_r9, "spree_variants"."position" AS t1_r10, "spree_variants"."cost_currency" AS t1_r11, "spree_variants"."track_inventory" AS t1_
r12, "spree_variants"."tax_category_id" AS t1_r13, "spree_variants"."updated_at" AS t1_r14 FROM "spree_products" LEFT OUTER JOIN "spree_variants" ON "spree_variants"."product_id" =
"spree_products"."id" AND "spree_variants"."is_master" = ? AND "spree_variants"."deleted_at" IS NULL WHERE "spree_products"."deleted_at" IS NULL AND "variants"."cost_currency" = ?
[["is_master", "f"], ["cost_currency", "USD"]]
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: variants.cost_currency: SELECT "spree_products"."id" AS t0_r0, "spree_products"."name" AS t0_r1, "spree_produc
ts"."description" AS t0_r2, "spree_products"."available_on" AS t0_r3, "spree_products"."deleted_at" AS t0_r4, "spree_products"."slug" AS t0_r5, "spree_products"."meta_description" A
S t0_r6, "spree_products"."meta_keywords" AS t0_r7, "spree_products"."tax_category_id" AS t0_r8, "spree_products"."shipping_category_id" AS t0_r9, "spree_products"."created_at" AS t
0_r10, "spree_products"."updated_at" AS t0_r11, "spree_products"."promotionable" AS t0_r12, "spree_products"."meta_title" AS t0_r13, "spree_variants"."id" AS t1_r0, "spree_variants"
."sku" AS t1_r1, "spree_variants"."weight" AS t1_r2, "spree_variants"."height" AS t1_r3, "spree_variants"."width" AS t1_r4, "spree_variants"."depth" AS t1_r5, "spree_variants"."dele
ted_at" AS t1_r6, "spree_variants"."is_master" AS t1_r7, "spree_variants"."product_id" AS t1_r8, "spree_variants"."cost_price" AS t1_r9, "spree_variants"."position" AS t1_r10, "spre
e_variants"."cost_currency" AS t1_r11, "spree_variants"."track_inventory" AS t1_r12, "spree_variants"."tax_category_id" AS t1_r13, "spree_variants"."updated_at" AS t1_r14 FROM "spre
e_products" LEFT OUTER JOIN "spree_variants" ON "spree_variants"."product_id" = "spree_products"."id" AND "spree_variants"."is_master" = ? AND "spree_variants"."deleted_at" IS NULL
WHERE "spree_products"."deleted_at" IS NULL AND "variants"."cost_currency" = ?

请尝试

Spree::Product.includes(:variants).where('spree_variants.cost_currency = "USD"')

Spree::Product.includes('variants').where('spree_variants.cost_currency = "USD" ' )

Spree::Product.includes('variants').where(spree_variants: { cost_currency: 'USD' })