2016-06-03 3 views
1

У меня есть 4 модели:жадная загрузка в гнездовой ассоциации

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 

Как я могу загрузить профиль и нетерпеливый ProfileCategory нагрузки и ProfileSubject?

Profile.includes(profile_categories: :profile_subject) не работает.

Gem пуля шоу следующее уведомление в браузере:

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

А в логах сервера я могу видеть много запросов к таблице ProfileCategory как:

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

Благодарность

ответ

0

Попробуй один раз ниже

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

он не работает. – verrom

+0

Попробуйте этот раз ... – Mukesh

+0

Это тоже не работает. – verrom

0

Из вашего SQL-журнала кажется, что ProfileSubject загружен правильно. Добавьте то, что вы видите из драгоценного камня.

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

Это должно работать

Смежные вопросы