Reputation: 513
I have these model associations:
class User < ApplicationRecord
has_many taken_tests
.
.
.
end
class Test < ApplicationRecord
has_many questions
.
.
end
Now User.preload(:taken_tests)
preloads the tests.
Is there a way I can preload questions also along with the tests?
Something like:
User.preload(:taken_tests).preload(:questions)
?
Upvotes: 0
Views: 301
Reputation: 23661
Yes, you can preload
the chain of associations
User.preload(taken_tests: :questions)
This will load all the user's tests along with all the questions which belongs to those tests
You can chain the associations if you need if you have answers you can preload them too.
User.preload(taken_tests: [questions: :answers])
Upvotes: 1