# Polymorphic association..explain the extra query?

**URL:** <https://www.ruby-forum.com/t/polymorphic-association-explain-the-extra-query/161600>\
**Category:** Rails\
**Created:** [February 27, 2009, 6:59pm UTC](https://www.ruby-forum.com/t/polymorphic-association-explain-the-extra-query/161600 "2009-02-27T18:59:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![arpit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e99b99/32.png) [@arpit](https://www.ruby-forum.com/u/arpit)\
**Post date:** [February 27, 2009, 6:59pm UTC](https://www.ruby-forum.com/t/polymorphic-association-explain-the-extra-query/161600/1 "2009-02-27T18:59:46Z")

</div>

Can anyone explain to me the sql query done in the last step :

[http://pastie.org/402200](http://pastie.org/402200)

–  
Arpit J.

---

<div class="post-metadata">

**Author:** ![arpit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e99b99/32.png) [@arpit](https://www.ruby-forum.com/u/arpit)\
**Post date:** [February 27, 2009, 7:49pm UTC](https://www.ruby-forum.com/t/polymorphic-association-explain-the-extra-query/161600/2 "2009-02-27T19:49:22Z")

</div>

It has nothing to do with polymorphism. You’ll want to look into eager  
loading for your associations if you want to avoid those queries.

> **[\#22 Eager Loading - RailsCasts](http://railscasts.com/episodes/22-eager-loading)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

-e

---

<div class="post-metadata">

**Author:** ![arpit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e99b99/32.png) [@arpit](https://www.ruby-forum.com/u/arpit)\
**Post date:** [February 28, 2009, 12:46am UTC](https://www.ruby-forum.com/t/polymorphic-association-explain-the-extra-query/161600/3 "2009-02-28T00:46:39Z")

</div>

On Feb 27, 5:58 pm, Arpit J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Can anyone explain to me the sql query done in the last step :
> 
> [http://pastie.org/402200](http://pastie.org/402200)

Because activerecord isn’t super smart in this way and doesn’t fill in  
associations bidirectionally when they are loaded.

Fred

---

<div class="post-metadata">

**Author:** ![arpit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e99b99/32.png) [@arpit](https://www.ruby-forum.com/u/arpit)\
**Post date:** [February 28, 2009, 2:57am UTC](https://www.ruby-forum.com/t/polymorphic-association-explain-the-extra-query/161600/4 "2009-02-28T02:57:45Z")

</div>

So what’s the solution to that Fred ? I would have to load question also  
when I load the subquestion ? That would mean doing a different query  
twice  
for extracting the same object. Or is there any other way around ?

On Sat, Feb 28, 2009 at 5:15 AM, Frederick C. \<

---

<div class="post-metadata">

**Author:** ![arpit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e99b99/32.png) [@arpit](https://www.ruby-forum.com/u/arpit)\
**Post date:** [February 28, 2009, 12:58pm UTC](https://www.ruby-forum.com/t/polymorphic-association-explain-the-extra-query/161600/5 "2009-02-28T12:58:15Z")

</div>

On Feb 28, 1:57 am, Arpit J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> So what’s the solution to that Fred ? I would have to load question also  
> when I load the subquestion ? That would mean doing a different query twice  
> for extracting the same object. Or is there any other way around ?

There isn’t really good solution. There’s a plugin that attempts to  
address this (parental\_control), or you can fiddle around making ar  
believe it has already loaded it. The ar\_context plugin can also be  
helpful in this context.  
You can also make your include load it (ie :include =\> {:subquestion  
=\> :question} but that always seems a bit messy.  
You also do things semi manually ie instead of referencing  
foo.question, collect all the questions into a hash indexed by id and  
access your\_hash[foo.question\_id]

Fred

---

<div class="post-metadata">

**Author:** ![arpit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e99b99/32.png) [@arpit](https://www.ruby-forum.com/u/arpit)\
**Post date:** [February 28, 2009, 6:22pm UTC](https://www.ruby-forum.com/t/polymorphic-association-explain-the-extra-query/161600/6 "2009-02-28T18:22:30Z")

</div>

hmm Right… Thanks 🙂

On Sat, Feb 28, 2009 at 5:27 PM, Frederick C. \<  
removed\_email\_address@domain.invalid\> wrote:

> address this (parental\_control), or you can fiddle around making ar
> 
> > > Fred
> > 
> > > > –  
> > > > Arpit J.

–  
Arpit J.
