Polymorphic association..explain the extra query ?

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

http://pastie.org/402200

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.

http://railscasts.com/episodes/22

-e

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

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

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 ?

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

hmm Right… Thanks :slight_smile: