good episode. But what about delegate? Where is the differnence to your:
read_attribute("category_name") || category.name
Using delegate :name, :to => :category, :prefix => 'category' would simply do category.name rather than read the loaded/cached category_name attribute. AFAIK
delegate :name, :to => :category, :prefix => 'category'
Off 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 User has FeedItems. Each feed item is polymorphically related to one other model object e.g. TextPost, Picture, File, SpreadSheet and many more, via FeedItem's postable polymorphic property
So the problem here is with FeedItem: When showing User's FeedItems, I want to do something like:
@feed_items = current_user.feed_items.includes(:postable)
But since postable is 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: TextPost and Picture have comments (which I'd like to eagerly load), and SpreadSheet has 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.
First sign in through GitHub to post a comment.