# Find objects without children

**URL:** <https://www.ruby-forum.com/t/find-objects-without-children/90304>\
**Category:** Rails\
**Created:** [March 27, 2007, 5:21pm UTC](https://www.ruby-forum.com/t/find-objects-without-children/90304 "2007-03-27T17:21:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 27, 2007, 5:21pm UTC](https://www.ruby-forum.com/t/find-objects-without-children/90304/1 "2007-03-27T17:21:41Z")

</div>

Hello all,

I am a newbie and therefore I am currently discovering the delights of  
Activerecord. I have a collection of objects, some of which have  
children and some of which have not. I am trying to run a find which  
will retrieve all parent objects which have children and then return  
an array. I know there is some sleek way to achieve this, and not the  
cumbersome iterative approach which looks to be looming ahead.

Any help is mucho appreciated…

j

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 27, 2007, 8:17pm UTC](https://www.ruby-forum.com/t/find-objects-without-children/90304/2 "2007-03-27T20:17:29Z")

</div>

removed\_email\_address@domain.invalid wrote:

> I am a newbie and therefore I am currently discovering the delights of  
> Activerecord. I have a collection of objects, some of which have  
> children and some of which have not. I am trying to run a find which  
> will retrieve all parent objects which have children and then return  
> an array. I know there is some sleek way to achieve this, and not the  
> cumbersome iterative approach which looks to be looming ahead.

Parent.find :all, :select =\> ‘unique parents.\*’,  
:joins =\> ‘inner join children on children.parent\_id =  
parents.id’

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 28, 2007, 12:53pm UTC](https://www.ruby-forum.com/t/find-objects-without-children/90304/3 "2007-03-28T12:53:52Z")

</div>

Thanks for you reply,

I didn’t need to use a join I just ran the SQL through

Parent.find\_by\_sql(‘SELECT parents.id, parents.name FROM parents,  
children WHERE parents.id = parents.country\_id’)

I was hoping to use Active record instead of applying direct SQL but  
thems the breaks.

Laters,

j

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 28, 2007, 2:05pm UTC](https://www.ruby-forum.com/t/find-objects-without-children/90304/4 "2007-03-28T14:05:21Z")

</div>

removed\_email\_address@domain.invalid wrote:

> I didn’t need to use a join I just ran the SQL through
> 
> Parent.find\_by\_sql(‘SELECT parents.id, parents.name FROM parents,  
> children WHERE parents.id = parents.country\_id’)

The comma is just syntactic sugar for an inner join.

–  
We develop, watch us RoR, in numbers too big to ignore.
