# Help with a find

**URL:** <https://www.ruby-forum.com/t/help-with-a-find/196017>\
**Category:** Rails\
**Created:** [October 5, 2010, 3:05pm UTC](https://www.ruby-forum.com/t/help-with-a-find/196017 "2010-10-05T15:05:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pitosalas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@pitosalas](https://www.ruby-forum.com/u/pitosalas)\
**Post date:** [October 5, 2010, 3:05pm UTC](https://www.ruby-forum.com/t/help-with-a-find/196017/1 "2010-10-05T15:05:39Z")

</div>

I have a simple one to many relation:

class Precinct  
has\_many :districts  
…  
end

I would like to find all Precincts that have no districts in a finder.  
Is that possible?

I also am using the searchlogic gem (very nice) but even with that I  
could not concoct a one liner to do what I want.

Any ideas would be greatly appreciated!

- PIto

---

<div class="post-metadata">

**Author:** ![pitosalas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@pitosalas](https://www.ruby-forum.com/u/pitosalas)\
**Post date:** [October 5, 2010, 3:42pm UTC](https://www.ruby-forum.com/t/help-with-a-find/196017/2 "2010-10-05T15:42:36Z")

</div>

On Oct 5, 2:05 pm, Pito S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have a simple one to many relation:
> 
> class Precinct  
> has\_many :districts  
> …  
> end
> 
> I would like to find all Precincts that have no districts in a finder.  
> Is that possible?

You can do that with a left outer join (and then an isnull to filter  
for rows with no districts)

Fred

---

<div class="post-metadata">

**Author:** ![pitosalas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@pitosalas](https://www.ruby-forum.com/u/pitosalas)\
**Post date:** [October 5, 2010, 3:45pm UTC](https://www.ruby-forum.com/t/help-with-a-find/196017/3 "2010-10-05T15:45:11Z")

</div>

Frederick C. wrote:

> On Oct 5, 2:05ï¿½pm, Pito S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I have a simple one to many relation:
> > 
> > class Precinct  
> > ï¿½ has\_many :districts  
> > …  
> > end
> > 
> > I would like to find all Precincts that have no districts in a finder.  
> > Is that possible?
> 
> You can do that with a left outer join (and then an isnull to filter  
> for rows with no districts)
> 
> Fred

Thanks… I am not sure how to express that as Precinct.find\_… can  
you say?

– pito

---

<div class="post-metadata">

**Author:** ![pitosalas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@pitosalas](https://www.ruby-forum.com/u/pitosalas)\
**Post date:** [October 5, 2010, 3:50pm UTC](https://www.ruby-forum.com/t/help-with-a-find/196017/4 "2010-10-05T15:50:55Z")

</div>

Pito S. wrote:  
[…]

> Thanks… I am not sure how to express that as Precinct.find\_… can  
> you say?

In cases like that, I usually find it helpful to write the SQL first,  
then transform it into ActiveRecord find syntax.

> – pito

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
