Rails查询只能随机工作

时间:2015-10-09 14:09:38

标签: sql ruby-on-rails ruby-on-rails-4 activerecord

我有如下的Rails查询:

Delivery.includes(delivery_products: :product)
.where('delivery_products.delivery_count > ? or delivery_products.back_count > ?', 0, 0)
.references(delivery_products: :product)
.joins(:customer).where(customers: {category: 'Auslieferer'})
.where('delivery_date >= ? AND delivery_date <= ?', from_date, to_date)

当我重新启动Rails服务器时,查询工作正常。在某个时间点之后(当我访问Rails应用程序的其他页面时),相同的查询会导致错误。我不知道为什么这个查询只是随机的。

这是错误:

Mysql2::Error: Unknown column 'products.product_prices.price' in 'field list': 
SELECT `deliveries`.`id` AS t0_r0, 
`deliveries`.`delivery_date` AS t0_r1, `deliveries`.`price_group` AS t0_r2, 
`deliveries`.`customer_id` AS t0_r3, 
`deliveries`.`created_at` AS t0_r4, `deliveries`.`updated_at` AS t0_r5, 
`deliveries`.`invoice_id` AS t0_r6, `deliveries`.`user_id` AS t0_r7, 
`delivery_products`.`id` AS t1_r0, `delivery_products`.`product_id` AS t1_r1, `delivery_products`.`delivery_id` AS t1_r2, `delivery_products`.`delivery_count` AS t1_r3, 
`delivery_products`.`back_count` AS t1_r4, `delivery_products`.`price` AS t1_r5, `delivery_products`.`created_at` AS t1_r6, `delivery_products`.`updated_at` AS t1_r7, `products`.`id` AS t2_r0, 
`products`.`product_number` AS t2_r1, `products`.`name` AS t2_r2, `products`.`tax` AS t2_r3, `products`.`created_at` AS t2_r4, 
`products`.`updated_at` AS t2_r5, `products`.`deleted` AS t2_r6, `products`.`product_prices.price` AS t2_r7, `products`.`product_prices.price` AS t2_r8, 
`products`.`product_prices.price` AS t2_r9, `products`.`product_prices.price` AS t2_r10, `products`.`product_prices.price` AS t2_r11, 
`products`.`product_prices.price` AS t2_r12, `products`.`product_prices.price` AS t2_r13, `products`.`product_prices.price` AS t2_r14, 
`products`.`product_prices.price` AS t2_r15, `products`.`product_prices.price` AS t2_r16, `products`.`product_prices.price` AS t2_r17, 
`products`.`product_prices.price` AS t2_r18 
 FROM `deliveries` 
 INNER JOIN `customers` ON `customers`.`id` = `deliveries`.`customer_id` 
 LEFT OUTER JOIN `delivery_products` ON `delivery_products`.`delivery_id` = `deliveries`.`id` 
 LEFT OUTER JOIN `products` ON `products`.`id` = `delivery_products`.`product_id` 
 WHERE (delivery_products.delivery_count > 0 or delivery_products.back_count > 0) AND `customers`.`category` = 'Auslieferer' AND (delivery_date >= '2015-10-01 00:00:00.000000' AND delivery_date <= '2015-10-31 23:59:59.000000')

我不知道为什么Rails正在创建以下sql选择导致错误。原始查询不会产生这个:

`products`.`product_prices.price` AS t2_r7

0 个答案:

没有答案