Forum: Ruby on Rails Querying children optimized sql?

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.
bataras (Guest)
on 2007-01-19 17:30
(Received via mailing list)
Re-asking...

I have a parent with has_many children. The children have a column
'blah'. IF I do the following:

    parent.children.find_by_blah(42)

The sql exectued is:

    select * from blah where parent_id = 1 and blah = 42
    select * from blah where parent_id = 1

The first line of sql is what I want. The second appears to be
ActiveRecord loading up all of parent.children. This I don't want b/c
children is huge.

How to I use ActiveRecord to find the children "by_blah" without
causing ALL children to be selected from the DB?

thanks
Mark Reginald J. (Guest)
on 2007-01-19 17:31
(Received via mailing list)
bataras wrote:

>     parent.children.find_by_blah(42)
>
> The sql exectued is:
>
>     select * from blah where parent_id = 1 and blah = 42
>     select * from blah where parent_id = 1
>
> The first line of sql is what I want. The second appears to be
> ActiveRecord loading up all of parent.children. This I don't want b/c
> children is huge.

Somewhere later you must be referring to the naked parent.children
collection, just like in your earlier problem

http://groups.google.com/group/rubyonrails-talk/br...


--
We develop, watch us RoR, in numbers too big to ignore.
bataras (Guest)
on 2007-01-19 17:31
(Received via mailing list)
No i just want to use idiomatic rails to cause

  select * from blah where parent_id = 1 and blah = 42

without causing rails to also do a presumptive

  select * from blah where parent_id = 1

Instead of using parent.children.find_by_blah(42), should I just just
do Child.find(:all, :condition => ['parent = ? and blah = ?",
parent.id, 42] ?

Kind of funky to me.
This topic is locked and can not be replied to.