Demystifying eager loading with ActiveRecord

eager loading not working

Eager loading in ActiveRecord is a great feature. It prevents what’s called the “n+1″ problem. From the docs (emphasis mine):

Eager loading is a way to find objects of a certain class and a number of named associations. This is one of the easiest ways of to prevent the dreaded 1+N problem in which fetching 100 posts that each need to display their author triggers 101 database queries. Through the use of eager loading, the 101 queries can be reduced to 2.

The docs then give this example:


This can scare beginners, as it did to me when I started learning about eager loading. This query doesn’t magically work by itself. You must have the proper associations set up in your models. Once I started to think of that include clause in terms of the associations, everything made sense:

Comments 5

  1. Thierry B. wrote:

    Sorry, but your explanation is too short. It doesn’t make it clearer

    Posted 26 Jan 2010 at 10:46 am
  2. admin wrote:

    It helped me a lot as well as someone I work with.

    But do let me know what you don’t understand, I’d be glad to try and help.

    -Alan

    Posted 26 Jan 2010 at 3:09 pm
  3. Trent C. wrote:

    I have to agree with the first comment. This doesn’t make anything more clear. There is no explanation of any kind, so the title is misleading, since if someone is mystified by something, they will probably not be demystified without an explanation of some kind.

    This reads more like an “Aha! When I looked at this, I suddenly understood.” If someone is really mystified by eager loading, they are probably not going to suddenly understand anything from this article.

    Posted 26 Jan 2010 at 5:34 pm
  4. Some Guy wrote:

    What is the point of this blog entry? All it does is restate, verbatim, the development docs, and a provide trivial example.

    Posted 26 Jan 2010 at 5:34 pm
  5. admin wrote:

    I don’t know what proficiency level of Ruby/Rails you guys are, but it seems that you have at least a decent amount of experience. It also seems that eager loading already makes sense to you.

    I started Rails last summer and one of my development partners just started Rails about a month ago. Eager loading “clicked” for me a few weeks ago, and clicked for my partner yesterday.

    The docs do not provide a complete picture as they only show *some* of the associations. It is up to the seasoned Rails developer to assume the rest of them. (FYI, I’m not criticizing the docs–I like them a lot.)

    I filled in a few small missing pieces that for at least me and one other person, made a huge difference. I don’t know about you, but I spend a good deal of my debugging time trying to fix problems whose solutions are small, often, trivial changes.

    When first picking up Ruby/Rails, it is often the small things that bog down learning: “What are those @-signs for? or those colons? When do I use them?” A short and simple explanation is all that is needed to get the new learner back on his/her feet. But without such an explanation s/he could be stuck for hours instead of minutes.

    The length of the solution does not necessarily determine the significance of the problem.

    Posted 26 Jan 2010 at 6:15 pm

Post a Comment

Your email is never published nor shared. Required fields are marked *