2017-02-10 73 views
1

下面的查询如何在Rails的eager_load中执行左外连接?

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

实在是太慢了,我很想只返回没有相关发票已安装,因为这将使其运行速度更快。我会怎么做呢?此外,提高查询速度的任何想法都会有所帮助,适当的表格将被编入索引。

+0

你可以用'joins',只是在连接状态下写的,'连接(“关于内部连接的客户... ...'不管') – Iceman

+1

'我很想只返回没有关联发票的安装程序',那么为什么要用发票来eager_loading客户? –

+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)