# Eager loading of polymorphic associations

**URL:** <https://www.ruby-forum.com/t/eager-loading-of-polymorphic-associations/56505>\
**Category:** Rails\
**Created:** [March 31, 2006, 7:20pm UTC](https://www.ruby-forum.com/t/eager-loading-of-polymorphic-associations/56505 "2006-03-31T19:20:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hmans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f08c70/32.png) [@hmans](https://www.ruby-forum.com/u/hmans)\
**Post date:** [March 31, 2006, 7:20pm UTC](https://www.ruby-forum.com/t/eager-loading-of-polymorphic-associations/56505/1 "2006-03-31T19:20:49Z")

</div>

Polymorphic associations work great until I try to do some eager  
loading through :include. In my example, the interface name is  
“:imagehaver”. Here’s my little eagerly loading find line:

Image.find(params[:id], :include =\> :imagehaver)

All it gives me is an ActiveRecord::EagerLoadPolymorphicError with no  
further explanation. I haven’t been able to dig up anything useful  
through Google, so I’m worried (or hoping?) that it could just be my  
code. Any ideas?

Cheers.

–  
[http://www.mans.de](http://www.mans.de)

---

<div class="post-metadata">

**Author:** ![hmans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f08c70/32.png) [@hmans](https://www.ruby-forum.com/u/hmans)\
**Post date:** [March 31, 2006, 7:51pm UTC](https://www.ruby-forum.com/t/eager-loading-of-polymorphic-associations/56505/2 "2006-03-31T19:51:16Z")

</div>

Hi Hendrik,

Unless something has changed since I last looked at it, eager loaded  
associations are pulled in with a single select.

So if you think about it, AR can’t build the “from” portion of the  
select until _after_ it’s read the resultset to see what tables the  
polymorphic associations belong to.

## Regards, Trevor

Trevor S.  
[http://somethinglearned.com](http://somethinglearned.com)

---

<div class="post-metadata">

**Author:** ![hmans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f08c70/32.png) [@hmans](https://www.ruby-forum.com/u/hmans)\
**Post date:** [March 31, 2006, 7:57pm UTC](https://www.ruby-forum.com/t/eager-loading-of-polymorphic-associations/56505/3 "2006-03-31T19:57:38Z")

</div>

> So if you think about it, AR can’t build the “from” portion of the  
> select until _after_ it’s read the resultset to see what tables the  
> polymorphic associations belong to.

Gah! You are correct. I guess this is what happens when you spend too  
much time in shiny happy ActiveRecord land. 🙂 Thanks!

–

> **[Hendrik Mans](https://hendrik.mans.de/)**
>
> Hendrik Mans: Freelancer in Hamburg. Ruby on Rails, Elixir, Phoenix, JavaScript, React.js, Vue.js., Elm.
