Polymorphic association..explain the extra query?


#1

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

http://pastie.org/402200


Arpit J.


#2

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


#3

On Feb 27, 5:58 pm, Arpit J. removed_email_address@domain.invalid wrote:

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


#4

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. <


#5

On Feb 28, 1:57 am, Arpit J. 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


#6

hmm Right… Thanks :slight_smile:

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.