Helps
eager-loading nested polymorphic associations
We have a controller action that performs n+1 queries to present associated models as nested hashes. The typical solution is to use :include to eager-load associations. Unfortunately, the models are related polymorphically, and :include can’t eager-load nested polymorphic associations. What can we do?Interestings
Use postgres!
Because in rails it automatically puts a transaction around for DDL statements and rolls them back when things don’t work. Fixing this in mysql can be a huge pain.
Regarding the eager loading issue, I tend to do this (say a Comment has a Post or Page polymorphically):
@comments = Comment.all
Post::Preloader.new(@comments.select { |c| c.commentable_type == ‘Post’ }, :author)
Page::Preloader.new(@comments.select { |c| c.commentable_type == ‘Page’ }, :designer)
It’s ugly, but it works.
March 13, 2013 at 11:11 pm