2016-06-03 14 views
1

Ich habe 4 Modelle:Eager Laden in verschachtelte Vereinigung

class Profile < ActiveRecord::Base 
    has_many :profile_profile_categories, dependent: :destroy 
    has_many :profile_categories, through: :profile_profile_categories 
end 

class ProfileCategory < ActiveRecord::Base 
    belongs_to :profile_subject 
end 

class ProfileSubject < ActiveRecord::Base 
    has_many :profile_categories, dependent: :destroy 
end 

class ProfileProfileCategory < ActiveRecord::Base 
    belongs_to :profile_category 
    belongs_to :profile 
end 

Wie kann ich laden Profil und eifrig Last ProfileCategory und ProfileSubject?

Profile.includes(profile_categories: :profile_subject) funktioniert nicht.

Gem Kugel nächste Meldung im Browser:

ProfileCategory Load (2.6ms) SELECT "profile_categories".* FROM "profile_categories" WHERE "profile_categories"."profile_subject_id" = $1 [["profile_subject_id", 4]] 

Dank

Antwort

0

Versuchen Sie einmal unter:

user: verrom N+1 Query detected ProfileSubject => [:profile_categories] Add to your finder: :includes => [:profile_categories] 

Und in Server-Logs ich viele Anfragen zu Tisch ProfileCategory wie sehen

Profile.includes(profile_profile_categories: [{ profile_category: :profile_subject } ], :profile_categories) 
+0

es nicht funktioniert. – verrom

+0

Try This once ... – Mukesh

+0

Das funktioniert nicht auch. – verrom

0

Von Ihrem SQL-Protokoll scheint ProfileSubject korrekt geladen worden zu sein. Fügen Sie hinzu, was Sie von bullet gem sehen.

Profile.includes(:profile_categories, profile_categories: :profile_subject)

sollte diese Arbeit

Verwandte Themen