简体   繁体   中英

How to do a left outer join in a eager_load in Rails?

The following query

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

Is REALLY slow, I would love to only return the Installs that do not have associated invoices, as that would make it run faster. How would I go about doing that? Also any ideas on improving the speed of this query would be helpful, the appropriate tables are indexed.

Not sure how exactly the association is defined but here's my stab at it:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM