1

我有一些旧的遗留代码,我已经添加了额外的参数(我已经删除了不必要的部分):

Farm.joins("LEFT JOIN ( SELECT farm_id, SUM(kwota) as suma_wplat FROM payments GROUP BY farm_id) as kwoty ON kwoty.farm_id = farms.id").
      select("farms.id, kwoty.suma_wplat").includes(:certificate).where("certificates.data_wyslania IS null")

这将产生这样的查询:

"SELECT farms.id, kwoty.suma_wplat FROM `farms` LEFT JOIN ( SELECT farm_id, SUM(kwota) as suma_wplat FROM payments GROUP BY farm_id) as kwoty ON kwoty.farm_id = farms.id WHERE (certificates.data_wyslania IS null)"

不幸的是,这将失败,因为suma_wplat以后不可用(返回的对象包含所有Farm属性)。

如果我将更includes改为joins生成的查询将如下所示:

 "SELECT farms.id, kwoty.suma_wplat FROM `farms` INNER JOIN `certificates` ON `certificates`.`farm_id` = `farms`.`id` AND `certificates`.`year` = '2013' LEFT JOIN ( SELECT farm_id, SUM(kwota) as suma_wplat FROM payments GROUP BY farm_id) as kwoty ON kwoty.farm_id = farms.id WHERE (certificates.data_wyslania IS null)"

现在Farm由适当的字段(连同 counted sum suma_wplat)组成,但它会导致 N+1 个查询。

我该如何解决?现在我只能考虑添加另一列Farm与计数值,但这将是一种解决方法,而不是真正的解决方案。

编辑:整个查询现在看起来像这样:

Farm.joins("LEFT JOIN ( SELECT farm_id, SUM(kwota) as suma_wplat FROM payments GROUP BY farm_id) as kwoty ON kwoty.farm_id = farms.id").select("farms.id, kwoty.suma_wplat").map do |f|
  (...)
  f.suma_wplat
end
4

0 回答 0