I have a table of restaurants that have many reservations. Now I would like to query all restaurants and in case they have reservations for 2 people it should preload these associations. In case there are no reservations for 2 it should still return the restaurants, but with an empty association.
For that I tried the following query:
Restaurant.eager_load(:reservations).where("number_of_people = ?", 2)
However, this doesn't work, since it discards all restaurants that do have reservations but none of them is for 2 people.
So what I would like to do is moving that condition into the join condition. Something like:
Restaurant.joins('LEFT OUTER JOIN \"reservations\" ON \"reservations\".\"restaurant_id\" = \"restaurants\".\"id\" AND \"reservations\".\"number_of_people\" = ?', 2)
That would give the result I need, however this is not preloading the "reservations" associations but causing an N+1 issue.
It doesn't seem like eager_load accepts custom queries. I found these threads: https://github.com/rails/rails/issues/12270 and JOIN ON ... AND conditions when eager loading association in ActiveRecord but there is no solution offered.