# Named\_scope on has\_many, find all records that don't have elements in that association

**URL:** <https://www.ruby-forum.com/t/named-scope-on-has-many-find-all-records-that-dont-have-elements-in-that-association/157468>\
**Category:** Rails\
**Created:** [January 11, 2009, 11:33pm UTC](https://www.ruby-forum.com/t/named-scope-on-has-many-find-all-records-that-dont-have-elements-in-that-association/157468 "2009-01-11T23:33:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![gabriele](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/76d3ee/32.png) [@gabriele](https://www.ruby-forum.com/u/gabriele)\
**Post date:** [January 11, 2009, 11:33pm UTC](https://www.ruby-forum.com/t/named-scope-on-has-many-find-all-records-that-dont-have-elements-in-that-association/157468/1 "2009-01-11T23:33:02Z")

</div>

Hi,  
I have a Customer model that has\_many :addresses, is there a way (via  
named scope or any other method that’s … ehm … model side) to  
obtain all the Customers that don’t have any addresses at all (for  
which the associated array would be empty)?

Thank you for the answer…

---

<div class="post-metadata">

**Author:** ![gabriele](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/76d3ee/32.png) [@gabriele](https://www.ruby-forum.com/u/gabriele)\
**Post date:** [January 12, 2009, 12:21am UTC](https://www.ruby-forum.com/t/named-scope-on-has-many-find-all-records-that-dont-have-elements-in-that-association/157468/2 "2009-01-12T00:21:43Z")

</div>

Thinking in SQL, you want to do this:

select \* from customers left outer join addresses on  
customers.address\_id = addresses.id  
where addresses.id is null.

First thing that comes to mind here something like:  
Customer.find(:all, :include =\> :address, :conditions =\> ‘addresses.id  
is null’)

hope that helps,  
-H

On Jan 11, 5:32 pm, Gabriele T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
