#22 Eager Loading (revised)
Mar 20, 2013 | 7 minutes | Performance, Active Record
One way to improve performance is to reduce the number of database queries through eager loading. Here I demonstrate this and compare the difference between the "includes" and "joins" methods.



good episode. But what about delegate? Where is the differnence to your:
Thank you
+1
Using
delegate :name, :to => :category, :prefix => 'category'would simply docategory.namerather than read the loaded/cachedcategory_nameattribute. AFAIKOff topic but how do you get SQL syntax highlighting when using tail?
I went through many of the original RailsCasts about a year ago. They where are a great way to refresh on the basics, but rails has changed so much that it would be difficult for a new rails hopeful, such as my son, to make some of them work. Thanks for giving us a fresh, back-to-basics episode.
I agree. They are they a good reminder for optimization techniques and what has been deprecated as we move move into new Rails versions.
Great episode! But I was hoping you'd touch eager loading with polymorphic associations. I couldn't find good information about it, and I feel it's a common problem.
The way I approached eager loading with polymorphic associations is to define the relationship as non-polymorphic for the types of polymorphic you need to eager load. Only works for cases where your eager loading is of the same type.
Yeah, but thats exactly what I wanted to avoid doing. My model is this message board:
A
UserhasFeedItems. Each feed item is polymorphically related to one other model object e.g.TextPost,Picture,File,SpreadSheetand many more, viaFeedItem'spostablepolymorphic propertySo the problem here is with
FeedItem: When showingUser'sFeedItems, I want to do something like:But since
postableis polymorphic, i cant and i get the n+1 problem...By using includes there, the polymorphic association is loaded in the best possible way (meaning that first it runs a query to load the feed items, and then, judging by the id/types it does a query for each type, and not for each instance, which from my point of view it's the best possible way to do it)
I had a more difficult problem posted here where I wanted to do an includes on the polymorphic model to eager-load other associated models, but ended up doing a manual sweep
You are totally correct, it really does what you said, which is the best solution. But I too share your problem:
TextPostandPicturehave comments (which I'd like to eagerly load), andSpreadSheethas collaborators, which I'd also like to eagerly load.+1 for eager loading with polymorphic associations. A very common and real problem that should be addressed in a Pro episode. Thanks.
+1
+1
+1
+100
First sign in through GitHub to post a comment.