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.
4012b0658c1e6e940414141efcf06ce6?d=identicon&s=25 Arpit Jain (Guest)
on 2009-02-27 18:59
(Received via mailing list)
Can anyone explain to me the sql query done in the last step :

http://pastie.org/402200

--
Arpit Jain
D93ae163042e0a937941e0ba4d8ed5d7?d=identicon&s=25 Eric (Guest)
on 2009-02-27 19: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
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-02-28 00:46
(Received via mailing list)
On Feb 27, 5:58 pm, Arpit Jain <arpitjai...@gmail.com> 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
4012b0658c1e6e940414141efcf06ce6?d=identicon&s=25 Arpit Jain (Guest)
on 2009-02-28 02: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 Cheung <
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-02-28 12:58
(Received via mailing list)
On Feb 28, 1:57 am, Arpit Jain <arpitjai...@gmail.com> 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
4012b0658c1e6e940414141efcf06ce6?d=identicon&s=25 Arpit Jain (Guest)
on 2009-02-28 18:22
(Received via mailing list)
hmm Right.. Thanks :)

On Sat, Feb 28, 2009 at 5:27 PM, Frederick Cheung <
frederick.cheung@gmail.com> wrote:

> address this (parental_control), or you can fiddle around making ar
> >
> > > Fred
> >
> > > > --
> > > > Arpit Jain
> >
>


--
Arpit Jain
This topic is locked and can not be replied to.