如何在Rails中的eager_load中执行左外连接?

时间:2017-02-10 21:35:15

标签: sql ruby-on-rails

以下查询

Install.eager_load(customer: :invoices).includes(calendar_appointment: :technician).where("state = 'scheduled' AND start_time < ?", Date.tomorrow)

真的很慢,我希望只返回没有关联发票的安装,因为这会使它运行得更快。我该怎么做呢?此外,任何提高此查询速度的想法都会有所帮助,相应的表将被编入索引。

1 个答案:

答案 0 :(得分:0)

不确定关联的定义是如何确定的,但这是我对它的抨击:

Install
.joins("inner join invoices on installs.id = invoices.install_id")
.includes(calendar_appointment: :technician)
.where("state = 'scheduled' AND start_time < ? AND invoices.install_id IS NULL", Date.tomorrow)