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:
parent
a462edb9bc
commit
8b8213c301
1 changed files with 2 additions and 0 deletions
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue