I’m struggling to find a comparison of includes() and preload() for ActiveRecord objects. Can anyone explain the difference ?
I’m struggling to find a comparison of includes() and preload() for ActiveRecord objects. Can
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Rails has 2 ways of avoiding the n+1 problem. One involves creating a big join based query to pull in your associations, the other involves making a separate query per association.
When you do
includesrails decides which strategy to use for you. It defaults to the separate query approach (preloading) unless it thinks you are using the columns from the associations in you conditions or order. Since that only works with the joins approach it uses that instead.Rails’ heuristics sometimes get it wrong or you may have a specific reason for preferring one approach over the other.
preload( and its companion methodeager_load) allow you to specify which strategy you want rails to use.