2017-03-23 33 views
0

我有這些模型關聯:如何在預加載軌道上執行預加載?

class User < ApplicationRecord 
    has_many taken_tests 
. 
. 
. 
end 

class Test < ApplicationRecord 
    has_many questions 
. 
. 
end 

現在User.preload(:taken_tests)預壓試驗。 有沒有一種方法可以預先加載問題以及測試? 類似於: User.preload(:taken_tests).preload(:questions)

+0

好的,有一件事是我做過的。我給用戶添加了另一個關聯:'has_many回答了問題,通過::taken_tests,來源:questions'。 然後,當我做了'User.preload(:answers_questions)',它預先加載了測試以及問題。 – vantony

回答

1

是的,你可以preload協會鏈

User.preload(taken_tests: :questions) 

這將加載所有的用戶一起測試的所有問題屬於那些測試

,如果你需要,如果你能鏈協會你也可以預先加載他們的答案。

User.preload(taken_tests: [questions: :answers])