2017-02-10 74 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)