RailsCasts Pro episodes are now free!

Learn more or hide this

Carlos Júnior's Profile

GitHub User: xjunior

Site: http://xjunior.me

Comments by Carlos Júnior

Avatar

Just to add up more info for future visitors:

You can, instead of "include" use "preload". But this will cause a second query to be triggered. This method is recommended since it'll help you to avoid N+1 queries.

Avatar

Unfortunately eager loading polymorphic associations isn't actually possible. The polymorphic association holds a a class name and an id present on that class. The polymorphic associations, though, don't have a way to join them in a SQL query since SQL doesn't support any sort of meta-programing.

For instance:

Address is kept in addresses table, and has an addressable reference. In the database there would be "addressable_id" and "addressable_type". There's no way to use "addressable_type" to make an INNER JOIN in the SQL query.