instructeurs: always eager load the user relationship

Now that `Instructeur.email` is merely an alias to `instructeur.user.email`,
and we changed every occurence of `instructeurs.pluck(:email)` to
`instructeurs.map(&:email)`, the new version using `map` may cause N+1 queries
if the users have not been preloaded.

It makes sense to always preload the user when fetching an Instructeur:

- Instructeur and User have a strongly coupled relationship
- It avoids N+1 queries everywhere in the app

Of course fetching an instructeur without needing its user will now do an
unecessary fetch of the associated user. But it seems better than leaving
a risk of N+1 queries in many places.
This commit is contained in:
Pierre de La Morinerie 2019-10-31 11:49:22 +01:00
parent a462edb9bc
commit 8b8213c301

View file

@ -24,6 +24,8 @@ class Instructeur < ApplicationRecord
has_one :user
default_scope { eager_load(:user) }
def self.by_email(email)
Instructeur.eager_load(:user).find_by(users: { email: email })
end