polymorphic association で Eager Loading
polymorphic association使おうとしたらEagerLoadできないでやんの。
いろいろ探してたら、
http://software.haruska.com/2007/08/eager-loading-polymorphic-associations.html
このページ見つけたのだけど、ネストした:includeが使えない。
おしい。非常におしい。
と思っていたら、タイミングが良いことに昨日本家にパッチがあがっていた。
There is an error condition with nested includes in this situation:
ShapeExpression.find :all, :include => [:shape, { :paint => :non_poly } ]In the include used above, :paint is a polymorphic relationship, therefore, the association to :non_poly is not necessarily the same for all :paint objects. However, as currently implemented, the eager include code will try to load all :non_poly associations using the association information from the first :paint object it checks. The test 'eager_load_nested_include_test.rb' demonstrates the problem using a simple object graph (with class defs provided at the bottom of the file).
The patched version of AssociationPreload#preload_one_association first sorts the record by class then preloads each group of sorted records.
そうそう。これがしたかったんよね。
試してみる。