Sql Rails查询只能随机工作

Sql Rails查询只能随机工作,sql,ruby-on-rails,ruby-on-rails-4,activerecord,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: 'Au

我有一个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)

不确定发生了什么,但在某些请求上的模型行为更改听起来像是由重新加载触发的。若要查看这是否正确,可以尝试以下操作:临时将config.cache_classes=true放入config/environments/development.rb中,重新启动服务器,然后查看是否获得一致的行为。尝试了。没用。我猜想Rails正在缓存另一个查询,然后将其与上面的查询混合。可能是这样吗?随机性总是很难处理的。第一步是尝试持续回购。尝试查找此查询开始中断的触发器,以便找出根本原因。否则,它只是猜测和检查,这是一个可怕的调试方式!为什么Rails要生产这么多产品。首先,产品价格与t2价格一样高。这可能是Rails的错误吗?我这样问是因为在上面的查询中,我没有要求Rails获取product_prices表,但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')
`products`.`product_prices.price` AS t2_r7