Forum: Ruby on Rails Polymorphic association..explain the extra query ?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Arpit J. (Guest)
on 2009-02-27 19:59
(Received via mailing list)
Can anyone explain to me the sql query done in the last step :

http://pastie.org/402200

--
Arpit J.
Eric (Guest)
on 2009-02-27 20:49
(Received via mailing list)
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
Frederick C. (Guest)
on 2009-02-28 01:46
(Received via mailing list)
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
Arpit J. (Guest)
on 2009-02-28 03:57
(Received via mailing list)
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. <
Frederick C. (Guest)
on 2009-02-28 13:58
(Received via mailing list)
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
Arpit J. (Guest)
on 2009-02-28 19:22
(Received via mailing list)
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.
This topic is locked and can not be replied to.