Forum: Ruby on Rails can preloading use just a single query per table (even if referenced multiple times)

D5935dc6e89bb3be00fbae0eed0118ae?d=identicon&s=25 Yavor Nikolov (Guest)
on 2014-05-01 21:02
(Received via mailing list)
Hi,

Let's take a look at the following example:

Visitor.preload(
  {allowed_meals: [:meal]}
  {meal_availabilities: [:meal]}
)


What happens is that Rails (I'm using version 4.1) is issuing several
queries to meals:

    Meals Load ... SELECT * from meals where id in (1, 2, 3, 4, 5) #
using
list of ids relevant for "allowed_meals"
    Meals Load ... SELECT * from meals where id in (3, 4, 5, 6) # using
list of ids relevant for "meal_availabilities"


For performance reasons - it would be nice to issue just a single query
instead of above two, e.g.:
    Meals Load ... SELECT * from meals where id in (1, 2, 3, 4, 5, 6)

Is that possible?

Regards,
Yavor
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.