# How to find models that have at least one associated has\_many

**URL:** <https://www.ruby-forum.com/t/how-to-find-models-that-have-at-least-one-associated-has-many/190041>\
**Category:** Rails\
**Created:** [June 11, 2010, 8:34pm UTC](https://www.ruby-forum.com/t/how-to-find-models-that-have-at-least-one-associated-has-many/190041 "2010-06-11T20:34:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rykkers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e5b9ba/32.png) [@rykkers](https://www.ruby-forum.com/u/rykkers)\
**Post date:** [June 11, 2010, 8:34pm UTC](https://www.ruby-forum.com/t/how-to-find-models-that-have-at-least-one-associated-has-many/190041/1 "2010-06-11T20:34:55Z")

</div>

I’m having a senior moment. I’ve done this before but I can’t remember  
how.

Two active record models,

Foo belongs\_to Bar

Bar has\_many Foos

I want to find all Bars that have at least one Foo, is it:

Bar.all(:include =\> :foos, :conditions =\> "foo.id IS NOT NULL’)

or something else?

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Github: [rubyredrick (Rick DeNatale) · GitHub](http://github.com/rubyredrick)  
Twitter: @RickDeNatale  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![rykkers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e5b9ba/32.png) [@rykkers](https://www.ruby-forum.com/u/rykkers)\
**Post date:** [June 11, 2010, 8:39pm UTC](https://www.ruby-forum.com/t/how-to-find-models-that-have-at-least-one-associated-has-many/190041/2 "2010-06-11T20:39:01Z")

</div>

Hi Rick,

On Fri, Jun 11, 2010 at 1:26 PM, Rick DeNatale [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Bar.all(:include =\> :foos, :conditions =\> "foo.id IS NOT NULL’)  
> Seems like it would be ‘foo.bar\_id’ in the condition clause.

HTH,  
Bill

---

<div class="post-metadata">

**Author:** ![rykkers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e5b9ba/32.png) [@rykkers](https://www.ruby-forum.com/u/rykkers)\
**Post date:** [June 11, 2010, 9:11pm UTC](https://www.ruby-forum.com/t/how-to-find-models-that-have-at-least-one-associated-has-many/190041/3 "2010-06-11T21:11:48Z")

</div>

On 11 June 2010 19:26, Rick DeNatale [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m having a senior moment. I’ve done this before but I can’t remember how.
> 
> I want to find all Bars that have at least one Foo, is it:
> 
> Bar.all(:include =\> :foos, :conditions =\> "foo.id IS NOT NULL’)
> 
> or something else?

Doesn’t…

Bar.all(:joins =\> :foos).uniq

…do the job?

---

<div class="post-metadata">

**Author:** ![rykkers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e5b9ba/32.png) [@rykkers](https://www.ruby-forum.com/u/rykkers)\
**Post date:** [June 11, 2010, 9:15pm UTC](https://www.ruby-forum.com/t/how-to-find-models-that-have-at-least-one-associated-has-many/190041/4 "2010-06-11T21:15:19Z")

</div>

On 11 June 2010 20:10, Michael P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Bar.all(:joins =\> :foos).uniq

Seems to… quick and dirty test on one of my models:

> > Person.all(:include =\> :graduations).select { |person| !person.graduations.empty? }.size  
> > =\> 87  
> > Person.all(:include =\> :graduations).select { |person| person.graduations.size \> 0 }.size  
> > =\> 87  
> > Person.all(:joins =\> :graduations).size  
> > =\> 103  
> > Person.all(:joins =\> :graduations).uniq.size  
> > =\> 87
